切换复选框的点击问题

发布于 2024-10-06 02:45:06 字数 380 浏览 1 评论 0原文

请找到下面的代码

$("#chkCopy").toggle(function () {
   copyDetails(); 
},
function () {
   emptyCopyDetails();
});

// functions which do not have any effect on the checkbox
function copyDetails() {

}

function emptyCopyDetails() {
}

问题是复选框未显示选中状态。

演示

提前致谢

Please find the code below

$("#chkCopy").toggle(function () {
   copyDetails(); 
},
function () {
   emptyCopyDetails();
});

// functions which do not have any effect on the checkbox
function copyDetails() {

}

function emptyCopyDetails() {
}

The issue is that checkbox is not showing the checked state.

Demo

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

调妓 2024-10-13 02:45:06

.toggle() 会中断检查,因为 .toggle() 实际上是.toggle() com/jquery/jquery/blob/1.4.4/src/event.js#L987" rel="nofollow">在 click 事件上调用 e.preventDefault()下面。我不建议采用这种方法,而是推荐这样的方法:

$("#chkCopy").change(function () {
  if(this.checked) {
    copyDetails();
  } else {
    emptyCopyDetails();
  }
});

这里是包含上述内容的演示的更新/工作版本方法

The .toggle() interrupts the check, since .toggle() actually calls e.preventDefault() on the click event underneath. Instead of that approach, I'd recommend something like this instead:

$("#chkCopy").change(function () {
  if(this.checked) {
    copyDetails();
  } else {
    emptyCopyDetails();
  }
});

Here's an updated/working version of your demo with the above approach.

金橙橙 2024-10-13 02:45:06
$("#chkCopy").change(function () {                alert("1");

            },
            function () {
                alert("2");
            });

使用更改而不是切换

$("#chkCopy").change(function () {                alert("1");

            },
            function () {
                alert("2");
            });

use change instead of toggle

笛声青案梦长安 2024-10-13 02:45:06

来自 文档

由于 .toggle() 内部使用
单击处理程序要完成其工作,我们必须
取消绑定点击以删除行为
附加 .toggle(),所以其他
click 处理程序可以在
交火。 实施还
在事件上调用 .preventDefault()
因此链接将不会被跟踪并且
如果出现以下情况,按钮将不会被点击

.toggle() 已被调用
元素。

因此,如果您希望选中和取消选中复选框,则不能使用 toggle()。您可以使用 click() 代替:

$("#chkCopy").click(function() {
   if (this.checked) {
       copyDetails();
   } else {
       emptyCopyDetails();
   }
});

From the documentation:

Since .toggle() internally uses a
click handler to do its work, we must
unbind click to remove a behavior
attached with .toggle(), so other
click handlers can be caught in the
crossfire. The implementation also
calls .preventDefault() on the event,
so links will not be followed and
buttons will not be clicked
if
.toggle() has been called on the
element.

So you can't use toggle() if you want the check box to be checked and unchecked. You can use click() instead:

$("#chkCopy").click(function() {
   if (this.checked) {
       copyDetails();
   } else {
       emptyCopyDetails();
   }
});
折戟 2024-10-13 02:45:06

看看这个答案,你会完成同样的事情

Jquery切换事件是弄乱复选框值

take a look at this answer you would accomplish the same thing

Jquery toggle event is messing with checkbox value

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文