jQuery单击复选框不检查在Firefox中 [英] jQuery click checkbox doesn't check in firefox

查看:117
本文介绍了jQuery单击复选框不检查在Firefox中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具体而言,输入类型复选框的内存区域在FireFox中不会收到checked属性(或任何其他已检查的指示符)。

注释行可以取消注释以查看测试通过,但是目前您会看到Firefox中的测试失败。



http://jsfiddle.net/mE3xb/2 /

  var cb = $('< input type =checkbox/>')
// var cb = $('input');
if(cb.is(':checked'))alert(checkbox said it has checked);
cb.click();
if(cb.is(':checked'))alert(checkbox clicked!);
else alert(fail!);

我在做非标准的事情吗?有什么建议?我基本上依靠jQuery .click来实际检查我的UI中的复选框(我不想使用.attr,.prop,.val,因为它会打破我很好的封装,我继续。)

解决方案

由于您使用的是jQuery 1.4.3,所以您最好的选择就是:

  cb [0] .setAttribute('checked','checked'); //普通旧香草JS 

jsFiddle Demo - 在FireFox& Chrome& IE8



.click()在Firefox中很多奇怪的情况下不起作用(这是天生的错误/不同的浏览器)。我会假设,因为它实际上不是在DOM上可用,所以它不知道这个事件将在哪里。这是诸如 .prop()之类的诸多原因之一。


Specifically, an in memory dom of input type checkbox does not receive the 'checked' attribute (or any other checked indicator) in FireFox.

The commented line can be uncommented to see the test pass, but currently you will see the test fail in firefox.

http://jsfiddle.net/mE3xb/2/

var cb = $('<input type="checkbox" />')
//var cb = $('input'); 
if (cb.is(':checked')) alert("checkbox says it's already checked"); 
cb.click(); 
if (cb.is(':checked')) alert("checkbox clicked correctly!"); 
else alert("fail!"); 

Am I doing non-standard things? Any advice? I'm essentially relying on the jQuery .click to actually check my checkbox in the UI (and I don't want to use .attr, .prop, .val because it will break my nice encapsulation that I got going on.)

解决方案

Since you are using jQuery 1.4.3, it looks like your best bet will be:

cb[0].setAttribute('checked', 'checked'); // plain old vanilla JS

jsFiddle Demo - tested in FireFox & Chrome & IE8

.click() doesn't work in many strange situations in Firefox (it is something inherently wrong/different in that browser). I would assume because it isn't actually available on the DOM yet, so it doesn't know where this event would be. This is one of the many reasons things like .prop() exist, etc.

这篇关于jQuery单击复选框不检查在Firefox中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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