此腳本會導致警報顯示,無論該複選框是打勾還是關閉。它如何才能在點擊複選框時顯示?當勾選複選框時,僅觸發javascript函數「開啓」
$("#x").click(function() {
alert("Ticker is has been clicked on!");
});
此腳本會導致警報顯示,無論該複選框是打勾還是關閉。它如何才能在點擊複選框時顯示?當勾選複選框時,僅觸發javascript函數「開啓」
$("#x").click(function() {
alert("Ticker is has been clicked on!");
});
您可以在函數內部添加if
語句來測試爲:
$("#x").click(function() {
if (this.checked) alert("Ticker is has been clicked on!");
});
//使用。是( ':檢查'),以檢查它是否被選中!
$("#x").click(function() {
if($(this).is(':checked')){
alert("Ticker is has been clicked on!");
}
});
一個簡單的if
條件會做的伎倆爲您提供:
$("#x").click(function(){
if($(this).is(":checked")) {
alert("Ticker is has been clicked on!");
}
});
檢查出內部的jQuery的.is() method和:checked selector一些閱讀。
令人驚歎! http://jsfiddle.net/f4Tmu/29/ – Starkers