[英]Can I use jquery methods,selectors in javascript function?
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 />
我的帶有jQuery的JavaScript函數,但無法正常工作。 當我單擊按鈕alert("hi");
調用此函數alert("hi");
被解雇但沒有alert(index);
還要解釋我的主要問題,也請向我解釋這個問題的方法
function ShowHideDxColumn() {
alert("hi");
$(".ckhdisplay").each(function (index) {
if ($(this).is(":checked")) {
alert(index);
}
});
}
感謝你
您有錯字, .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);
});
請嘗試以下方法:
$.each($(".chkdisplay"), function(index, element) {
if ($(this).attr('checked')){
alert(index);
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.