0

HTML:

<input id="otherCheckbox2" type="checkbox" value="Accepted" style="color: Black" class="chkdisplay" onchange="javascript:othercheckbxdisplay();" /> 
  <span style="color: Black">Accepted</span> <br />
<input id="otherCheckbox3" type="checkbox" value="Contracted" style="color: Black" class="chkdisplay" onchange="javascript:othercheckbxdisplay();" />
  <span style="color: Black">Contracted</span> <br />
<input id="otherCheckbox4" type="checkbox" value="Pending" style="color: Black" class="chkdisplay" onchange="javascript:othercheckbxdisplay();" />
   <span style="color: Black">Pending</span><br />
<input id="otherCheckbox5" type="checkbox" value="Pre-Authorized" style="color: Black"  class="chkdisplay" onchange="javascript:othercheckbxdisplay();" />
   <span style="color: Black">Pre-Authorized</span> <br />
<input id="otherCheckbox6" type="checkbox" value="Show Deleted" style="color: Black" class="chkdisplay" onchange="javascript:othercheckbxdisplay();" />
   <span style="color: Black">Show Deleted</span> <br />
<input id="otherCheckbox7" type="checkbox" value="Treated" style="color: Black" class="chkdisplay" onchange="javascript:othercheckbxdisplay();" />
  <span style="color: Black">Treated</span> <br />  

MY javascript function with jquery but it does not work. called this function on a button click.Wen i click on button alert("hi"); is fired but not alert(index); Also explain my main question and just me the way for this question

 function ShowHideDxColumn() {
                alert("hi");
                $(".ckhdisplay").each(function (index) {
                    if ($(this).is(":checked")) {
                        alert(index);
                    }
                });
            }

thank u

4

2 回答 2

3

你有错别字,.ckhdisplay应该是.chkdisplay。因此,您的.each调用没有要迭代的元素,因为给定的类没有元素。

 function ShowHideDxColumn() {
     alert("hi");
     $(".chkdisplay").each(function (index) {
         if ($(this).is(":checked")) {
             alert(index);
         }
     });
 }

您实际上并不需要每个条件中的条件,您只需选中选中的复选框:

$(".chkdisplay:checked").each(function(index){
    console.log(this);
});
于 2013-06-24T07:07:24.743 回答
0

请试试这个:

$.each($(".chkdisplay"), function(index, element) {
    if ($(this).attr('checked')){
        alert(index);
    }
});
于 2013-06-24T07:07:33.547 回答