简体   繁体   English

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

[英]Can I use jquery methods,selectors in javascript function?

HTML: 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. 我的带有jQuery的JavaScript函数,但无法正常工作。 called this function on a button click.Wen i click on button alert("hi"); 当我单击按钮alert("hi");调用此函数alert("hi"); is fired but not alert(index); 被解雇但没有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 感谢你

You have a typo, .ckhdisplay should be .chkdisplay . 您有错字, .ckhdisplay应该是.chkdisplay Because of that, your .each call has no elements to iterate over because there are none with the given class. 因此,您的.each调用没有要迭代的元素,因为给定的类中没有任何元素。

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

You actually don't really need the condiiton in the each, you can just select the checked checkboxes: 实际上,您实际上并不需要每个条件,您只需选中已选中的复选框即可:

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

Please try 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