使用jQuery触发选中和取消选中复选框的操作

使用jQuery触发选中和取消选中复选框的操作,jquery,forms,checkbox,checked,eventtrigger,Jquery,Forms,Checkbox,Checked,Eventtrigger,关于jQuery和复选框,有几个问题有很好的答案,还有堆栈溢出上可用的代码片段,但显然我误解了所有这些问题 我的表单中有以下HTML代码: <input type="checkbox" name="copyAddress" id="copyAddress" value="1" /> 根据上述情况,应该是可行的。我还尝试使用.trigger()、.click()等代替.change(),并使用if()代替逻辑运算符作为条件。在所有情况下,上面的Javascript代码都会被触发,但无

关于jQuery和复选框,有几个问题有很好的答案,还有堆栈溢出上可用的代码片段,但显然我误解了所有这些问题

我的表单中有以下HTML代码:

<input type="checkbox" name="copyAddress" id="copyAddress" value="1" />
根据上述情况,应该是可行的。我还尝试使用.trigger()、.click()等代替.change(),并使用if()代替逻辑运算符作为条件。在所有情况下,上面的Javascript代码都会被触发,但无论复选框处于选中或未选中状态,我都会收到“未选中”警报。换句话说,触发不是这里的问题,而是未正确检测(或处理)复选框的条件(选中或未选中状态)

显然我忽略了这里的一些东西。它是什么?

您可以这样使用:

$('#copyAddress').change(function () {
    $(this).prop('checked')
      ? alert ("Checked")
      : alert ("Unchecked");
  });

或:

您可以这样使用:

$('#copyAddress').change(function () {
    $(this).prop('checked')
      ? alert ("Checked")
      : alert ("Unchecked");
  });

或:

您可以这样使用:

$('#copyAddress').change(function () {
    $(this).prop('checked')
      ? alert ("Checked")
      : alert ("Unchecked");
  });

或:

您可以这样使用:

$('#copyAddress').change(function () {
    $(this).prop('checked')
      ? alert ("Checked")
      : alert ("Unchecked");
  });

或:

试试这个:

$(“#copyAddress”).change(函数(){
$(this).is(“:checked”)?警报(“checked”):警报(“Unchecked”);
});

jsiddle:

试试这个:

$(“#copyAddress”).change(函数(){
$(this).is(“:checked”)?警报(“checked”):警报(“Unchecked”);
});

jsiddle:

试试这个:

$(“#copyAddress”).change(函数(){
$(this).is(“:checked”)?警报(“checked”):警报(“Unchecked”);
});

jsiddle:

试试这个:

$(“#copyAddress”).change(函数(){
$(this).is(“:checked”)?警报(“checked”):警报(“Unchecked”);
});

jshiddle:

希望这对你有所帮助

$('#copyAddress').on('change', function(){
    if(this.checked){
        // Do some action when checked
    }else{
        // Do some action when unchecked
    }
});
希望这有帮助

$('#copyAddress').on('change', function(){
    if(this.checked){
        // Do some action when checked
    }else{
        // Do some action when unchecked
    }
});
希望这有帮助

$('#copyAddress').on('change', function(){
    if(this.checked){
        // Do some action when checked
    }else{
        // Do some action when unchecked
    }
});
希望这有帮助

$('#copyAddress').on('change', function(){
    if(this.checked){
        // Do some action when checked
    }else{
        // Do some action when unchecked
    }
});

使用javascript
this.checked
或jQuery
$(this.is(':checked')
使用javascript
this.checked
或jQuery
$(this.is(':checked')
使用javascript
this.is(':checked')
使用javascript
this.checked
或jQuery
$(this.is(':checked'))
就是这样。非常感谢。成功了。非常感谢。成功了。非常感谢。成功了。非常感谢。