2013-06-24 34 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); 
    } 
}); 
相關問題