繁体   English   中英

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

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM