设置$ .prop()而不触发更改 [英] Set $.prop() without triggering change

查看:122
本文介绍了设置$ .prop()而不触发更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<li>
 <input type="checkbox" id="master" />
   <ul>
   <li>
     <input type="checkbox" />

$('input').bind('change', function() {}); // exists elsewhere, cannot change

当我单击顶级输入时,我想为其所有子项设置.prop('checked', true),但是我想避免为每个子项复选框触发change事件.

When I click the top level input, I want to set .prop('checked', true) for all of its children, but I want to avoid triggering the change event for each child checkbox.

如何在不触发change的情况下将.prop('checked', true)设置为输入?

How could I set .prop('checked', true) to an input without triggering change?

(正在扩展)

$('#master').click(function() {
    $(this).parent().find('input').each(function(n, in) {
        $(in).prop('checked', true); // this triggers a change event that I need to stop
    });
});

推荐答案

您所描述的是默认功能.从javascript更改元素不会触发change事件.

What you are describing is the default functionality. Changing an element from javascript does not fire the change event.

http://jsfiddle.net/8JsP4/

请注意,如果您单击示例中的复选框,将如何获得警报,但是如果单击该按钮,则没有警报.

Notice how if you click on a checkbox in the example, you get an alert, but if you click the button, no alert.

这篇关于设置$ .prop()而不触发更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆