jQuery,checkboxes and .is(":checked") [英] jQuery, checkboxes and .is(":checked")

查看:227
本文介绍了jQuery,checkboxes and .is(":checked")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将一个函数绑定到一个复选框元素如:

  $(#myCheckbox ){
alert($(this).is(:checked));
});

复选框会在触发事件之前更改其选中的属性


$ b

但是,当我这样做:

 

code> $(#myCheckbox)。click();

复选框在触发事件后选中属性 p>

我的问题是,是否有一种方法来触发来自jQuery的点击事件,像正常的点击会做的(第一种情况)?



PS:我已经尝试过 trigger('click');

解决方案

我认为你应该使用更改处理程序:

  $('#myCheckbox' ).change(function(){
if($(this).attr(checked)){
// checked
return;
} / not checked
});


When I bind a function to a checkbox element like:

$("#myCheckbox").click( function() {
    alert($(this).is(":checked"));
});

The checkbox changes its checked attribute before the event is triggered, this is the normal behavior, and gives an inverse result.

However, when I do:

$("#myCheckbox").click();

The checkbox changes it checked attribute after the event is triggered.

My question is, is there a way to trigger the click event from jQuery like a normal click would do (first scenario)?

PS: I've already tried with trigger('click');

解决方案

I would think you should use the "change" handler instead:

$('#myCheckbox').change(function () {
    if ($(this).attr("checked")) {
        // checked
        return;
    }
    // not checked
});

这篇关于jQuery,checkboxes and .is(":checked")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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