选中复选框时执行函数

Execute function when the checkbox is checked

我有以下功能,当我们选中或取消选中复选框(在 gridview 内)时,它会显示弹出窗口和其他处理信息。

有什么方法可以仅在选中复选框而不是未选中的情况下执行此功能吗?

下面是我的函数:

 $(document).ready(function () {
    $('#<%= gvPRCertInfo.ClientID %> input[type="checkbox"]').change(function () {

        var signValue = $(this).closest('tr').children('td:eq(4)').html();

        if (signValue == "Virtual") {

            var confirm_value = document.createElement("INPUT");
            confirm_value.type = "hidden";
            confirm_value.name = "confirm_value";

            if (confirm("you have selected virtual do u want to create a new name for this?")) {
                confirm_value.value = "Yes";
            } else {
                confirm_value.value = "No";
            }
            document.forms[0].appendChild(confirm_value);
        }
    });
});

这是我的网格视图:

 <asp:GridView ID="gvPRCertInfo" runat="server" GridLines="None"                                                                                  
   CssClass="data responsive">
           <Columns>
          <asp:TemplateField HeaderText="Select" SortExpression="">
             <HeaderTemplate>
               <asp:CheckBox ID="chkboxSelectAll" runat="server" AutoPostBack="true" OnCheckedChanged="chkboxSelectAll_CheckedChanged" />
                </HeaderTemplate>
                   <ItemTemplate>
                    <asp:CheckBox ID="chkCert" AutoPostBack="true" ClientIDMode="Static" OnCheckedChanged="chkCert_CheckedChanged" runat="server" />
                    <input type="hidden" id="hdnCertId" runat="server" value='<%# DataBinder.Eval(Container.DataItem, "CertId") %>' />
                           </ItemTemplate>
                   </asp:TemplateField>
            <asp:BoundField DataField="CertificateID" HeaderText="Certificate ID" HeaderStyle-HorizontalAlign="Center" />

 ................
 .................

任何人都可以就如何仅在我选中复选框时执行提出任何想法吗?

在 onchange 函数中试试这个

Javascript代码

if(document.getElementById('mycheckbox').checked) {
    console.log("ckecked")
} else {
     console.log("not ckecked")
}

jquery代码

  if ($('#mycheckbox').attr("checked")) {
             console.log("ckecked")
  }else{    
    console.log("not ckecked")
  }

你好,亲爱的,我想这会奏效,但我仍然不确定

你可以像下面这样编码:

$(document).ready(function () {
    $('#myCheckbox').change(function () {
     if ($(this).attr("checked")) {
        // fire your function 
        return;
     }
     // not checked
   });
});

在复选框的更改功能中试试这个。

if($(this).is(':checked') == true)
{

/* Your function code here.*/

}

您可以使用 jQuery 的 is() 函数:

if($("input").is(':checked')) {
  dosomething
}