2013-06-24 31 views
0

HTML:我可以在javascript函数中使用jquery方法,选择器吗?

<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 /> 

我的JavaScript函数与jQuery,但它不工作。 叫上一个按钮click.Wen这个功能我点击按钮alert("hi");被激发,但不alert(index);也说明我的主要问题,只是我这个问题的方式

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

感谢ü

回答

3

你有一个错字,.ckhdisplay应该是.chkdisplay。正因为如此,你.each调用没有任何元素,因为有没有与给定的类遍历。

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

你其实并不需要在每个condiiton,你可以选择选中的复选框:

$(".chkdisplay:checked").each(function(index){ 
    console.log(this); 
}); 
+0

感谢您通知我,我的我真的犯了错误? –

+0

将ü解释我,我可以使用jQuery方法,选择在javascript函数? –

+1

是的,当然你可以使用jQuery方法和选择器在JavaScript函数,这正是它的设计使用。 – MrCode

0

请试试这个:

$.each($(".chkdisplay"), function(index, element) { 
    if ($(this).attr('checked')){ 
     alert(index); 
    } 
}); 
相关问题