简体   繁体   English

使用哪个jquery事件?

[英]Which jquery event to use?

I've got a code like this to enable or disable som text inputs when a checkbox is marked. 当有一个复选框被标记时,我有一个这样的代码来启用或禁用som文本输入。

$(document).ready(function(){
    $(":checkbox").change(function(){
        if(this.checked){
            $("input:text").attr('disabled', true);
        } else {
            $("input:text").attr('disabled', false);
        }
      });
});

It seems to work when the user clicks on the checkbox, but the javascript can change the checkbox value by invoking: 当用户点击复选框时,它似乎有效,但javascript可以通过调用来更改复选框值:

$(":checkbox").attr('checked', true); 

When the checkbox is changed by the javascript the event is not fired. 当javascript更改复选框时,不会触发事件。 I've tried with click() event but happens the same. 我尝试过click()事件但是发生了同样的事情。

Any idea? 任何的想法?

Thanks. 谢谢。

First, let's slim down that event handlers, since you're passing a boolean to .attr('disabled', bool) and .checked is a boolean, let's take advantage of it, like this: 首先,让我们减少那些事件处理程序,因为你将布尔值传递给.attr('disabled', bool)并且.checked是一个布尔值,让我们利用它,如下所示:

$(function(){
  $(":checkbox").change(function(){
    $("input:text").attr('disabled', this.checked);
  });
});

Then when changing the value programmatically just trigger the change event as well, like this: 然后以编程方式更改值时,也会触发change事件,如下所示:

$(":checkbox").attr('checked', true).change();

.change() without parameters is a shortcut for .trigger("change") , which will cause your event handler to run. 没有参数的.change().trigger("change")的快捷方式,这将导致您的事件处理程序运行。

手动调用change事件:

$(":checkbox").change();

很抱歉,如果我太简单了,但是当你更改复选框值时,你不能让JavaScript手动调用该事件吗?

You could try to fire the event manually: 您可以尝试手动触发事件:

$(":checkbox").attr('checked', true);
$(":checkbox").change();

or 要么

$(":checkbox").attr('checked', true).change();

You can just .trigger() the change event manually: 您可以手动将.trigger() change event

$('input:checkbox').trigger('change');

You should never use a selector like :checkbox . 你永远不应该使用像:checkbox这样的选择器。 It's an implicit usage of the universal * selector which will query all nodes on your site, which is pretty bad, which means it's pretty slow. 它是通用*选择器的隐式用法,它将查询您站点上的所有节点,这非常糟糕,这意味着它非常慢。

input:checkbox will still query all of your checkbox elements, you should us a class or an id to query for a specific element. input:checkbox仍将查询所有复选框元素,您应该使用classid来查询特定元素。

Ref.: .trigger() 参考: .trigger()

I don't know why changing the checkbox through javascript doesn't fire the event. 我不知道为什么通过javascript更改复选框不会触发事件。 But, i have a solution for this. 但是,我有一个解决方案。

make 使

function(){
        if(this.checked){
            $("input:text").attr('disabled', true);
        } else {
            $("input:text").attr('disabled', false);
        }
      }

as a function. 作为一个功能。 Such as function cb_change_handler(){ ...} and u can manually use this function when changing the value by javascript. 比如函数cb_change_handler(){...}和u可以在通过javascript更改值时手动使用此函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM