在.NET转发器内部的复选框上触发JQuery事件 [英] Triggering JQuery event on a checkbox inside of .NET repeater

查看:79
本文介绍了在.NET转发器内部的复选框上触发JQuery事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个中继器,其中有一个复选框.

I have a repeater with a checkbox inside of it.

<tr class="datarow">
      <td style="display: none">
           <asp:HiddenField runat="server" ID="hfPortalUserId" Value='<%# Eval("portalUserId") %>' />
      </td>
      <td>
           <asp:TextBox ID="tbUserName" runat="server" Enabled="false" Text='<%# Eval("portalCommonName") %>' Width="98%"></asp:TextBox>
      </td>
      <td>
           <asp:CheckBox ID="chkActive" Enabled="false" runat="server" Checked='<%# IsPortalUserActive((DateTime)Eval("portalUserInactive")) %>' />
      </td>
      <td>
           <asp:CheckBox ID="chkEmployee" Enabled="false" runat="server" Checked='<%# IsEmployee((int)Eval("portalUserTypeId")) %>' />
      </td>
      <td class="checkadmin">
           <asp:CheckBox ID="chkAdmin" Enabled="false" runat="server" Checked='<%# IsAdministrator((int)Eval("siteSecurityRoleId")) %>' CssClass="chkadmin" />
      </td>
      <td>
           <asp:DropDownList ID="ddlUserClient" Enabled="false" runat="server" Width="98%" ></asp:DropDownList>
           <asp:HiddenField runat="server" ID="hfClientId" Value='<%#Eval ("clientId") %>' />
       </td>
       <td>
           <asp:TextBox ID="tbEmail" runat="server" Enabled="false" Text='<%# Eval("portalEmail") %>' Width="98%" AutoCompleteType="None"></asp:TextBox>
       </td>
       <td>
           <asp:TextBox ID="tbPassword" runat="server" Enabled="false"  Width="98%" AutoCompleteType="None" ></asp:TextBox>
            <asp:TextBoxWatermarkExtender ID="wmPass" runat="server" TargetControlID="tbPassword" WatermarkText="Enter new Password"></asp:TextBoxWatermarkExtender>
        </td>
        <td>
            <asp:Panel ID="pnlControls" runat="server" Visible="true">
                <asp:ImageButton ID="btnEdit" ImageUrl="~/Images/Management/EditDocument_20x20.png" runat="server" ToolTip="Update User" CommandName="edit" CausesValidation="false" />
                 <asp:ImageButton ID="btnDelete" ImageUrl="~/Images/Management/Delete_black_20x20.png" runat="server" CommandName="delete" ToolTip="Mark User Inactive" CausesValidation="false" OnClientClick="return confirm('Are you sure you would like to delete this user');" />
             </asp:Panel>
         </td>
</tr>

呈现控件时,控件将被禁用,并且单击编辑"按钮时,将启用该行的控件.我正在尝试使用JQuery选择器来捕获管理"复选框的单击事件以及弹出框和警报框.

When the control is rendered the controls are disabled and when the edit button is clicked, the row's controls are enabled. I am trying to use a JQuery selector to catch the Admin checkbox click event and and popup and alert box.

这是JQuery代码

$("input[id^=BodyLeft_rptUsers_chkAdmin]").change(function () {
    alert('Event Fired');
    if ($(this).is(':checked')) {
        alert('You are about to give administrator privileges. Are you sure?');
    } 
});

如果我使用IE开发人员工具并在页面渲染后运行此脚本,则该脚本可以运行,但否则不会触发该事件,也不会显示警报框.

If I use IE Developer Tools and run this script after the page render, it works, but otherwise the event doesn't fire and no alert box.

推荐答案

将其包装在$(function(){ })内,这将确保在jQuery尝试找到所需元素之前已加载页面.

Wrap it inside $(function(){ }) which will ensure that the page is loaded before jQuery tries to find the required element.

$(function(){
   $(".chkadmin").click(function () {
       alert('Event Fired');
       if ($(this).is(':checked')) {
          alert('You are about to give administrator privileges. Are you sure?');
       } 
   });
});

这篇关于在.NET转发器内部的复选框上触发JQuery事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆