-1

I have written a function for a hdcheck checkbox.

$('#hdcheck').change(function () {
  if($(this).is(':checked')) {
    alert("It is Checked");
  }
  else {
    alert("It is Unchecked");
  }
});

It works find when I check or uncheck the checkbox but I have made a function that problematically make this checkbox checked or unchecked. Here is the function on checkbox chk_1:

$('#chk_1').change(function () {
  if($(this).is(':checked')) {
    $('#hdcheck').prop("checked", true);
  }
  else {
    $('#hdcheck').prop("checked", false);
  }
});

This function works fine on making the first checkbox (hdcheck) checked or unchecked but it does not pop ups the alert message. Mean the function $('#hdcheck').change(function() does not work.

4

1 回答 1

2

当您以编程方式更改属性时,您应该触发更改事件,这是代码的缩小版本 + 更改触发器。

$('#chk_1').change(function() {
    $('#hdcheck').prop("checked", this.checked).change();
});

这不是错误,这是预期的行为。

于 2013-03-09T15:31:49.713 回答