0

无论复选框是打开还是关闭,此脚本都会显示警报。它如何仅在单击复选框时显示?

$("#x").click(function() {
    alert("Ticker is has been clicked on!");
});

JSFIDDLE

4

3 回答 3

2

您可以在函数内添加一条if语句来测试:

$("#x").click(function() {
    if (this.checked) alert("Ticker is has been clicked on!");
});
于 2013-07-29T01:42:46.343 回答
1

// 使用 .is(':checked') 来检查它是否被检查!

$("#x").click(function() {
    if($(this).is(':checked')){
        alert("Ticker is has been clicked on!");
    }
});
于 2013-07-29T01:42:56.253 回答
0

一个简单的if条件将为您解决问题:

$("#x").click(function(){
    if($(this).is(":checked")) {
        alert("Ticker is has been clicked on!");
    }
});

查看jQuery中.is() 方法:checked 选择器的一些内容。

于 2013-07-29T01:44:53.170 回答