jQuery 统一复选框不(取消)检查
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6637723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery Uniform Checkbox does not (un)check
提问by nderjung
I am using jQuery Uniformto style inputs/selects etcs. However, the checkbox has stopped working. I am appending
data sent from an ajax call. Once it's loaded, I use $.uniform.update("input:checkbox")
to update the new html. When attempting to (un)check the input it works only once. If I want to (un)check it again, it doesn't change at all.
我正在使用jQuery Uniform来设计输入/选择等。但是,该复选框已停止工作。我是appending
从 ajax 调用发送的数据。加载后,我会使用它$.uniform.update("input:checkbox")
来更新新的 html。当尝试(取消)检查输入时,它只工作一次。如果我想(取消)再次检查它,它根本不会改变。
I've attempted changing the Uniform Javascriptso that all the actions (ie. click
, focus
, blur
etc) are under the .live
function. (ie. .live("click",
). This just stops anything from working.
我已经尝试改变统一的Javascript,使所有的动作(即click
,focus
,blur
等等)是下.live
功能。(即。.live("click",
)。这只会阻止任何工作。
Update:
更新:
I've read through the Uniform JS entirely and discovered a problem:
我已经通读了 Uniform JS 并发现了一个问题:
if(!$(elem).attr("checked")){
//box was just unchecked, uncheck span
spanTag.removeClass(options.checkedClass);
}else{
//box was just checked, check span.
spanTag.addClass(options.checkedClass);
}
The .attr("checked")
syntax fails. It will always return false. Why? I don't know. As well as this, it doesn't update the original input
to either remove the checked
attribute or to set the checked
attribute.
该.attr("checked")
语法失败。它总是会返回false。为什么?我不知道。除此之外,它不会更新原始属性以input
删除checked
属性或设置checked
属性。
I've run through the official website and found it doesn't update the checkbox input. So it's not just me xP
我浏览了官方网站,发现它没有更新复选框输入。所以不仅仅是我 xP
I've Googled around a bit and found various methods for checking if a checkbox is checked however the following methods fail:
我在谷歌上搜索了一下,发现了各种检查复选框是否被选中的方法,但是以下方法失败了:
if ($(elem).attr("checked")) // The original.
if ($(elem).attr("checked") == true)
if ($(elem).is(":checked"))
if ($(elem).checked)
// and:
var check = $(elem).match(/checked=/);
if (check == null)
Words of guidance are very much appreciated~ :3
非常感谢指导的话~ :3
回答by Mick
SIMPLE SOLUTION
简单的解决方案
This is because UniformJs
requires that you update your modification on uniform elements if you need to change values on the form dynamically:
这是因为UniformJs
如果您需要动态更改表单上的值,则要求您更新对统一元素的修改:
$('#checkbox').prop('checked',true);
$.uniform.update();
Edit
编辑
If you wish to update the #checkbox
only:
如果您#checkbox
只想更新:
$('#checkbox').prop('checked',true);
$.uniform.update('#checkbox');
回答by moledet
Just do this:
只需这样做:
$('#checkbox').prop('checked',true).uniform('refresh');
回答by John Goodman
I know this may be a bit late, but hopefully it will help someone. I had to add a couple lines to the uniform code so it would look for pre checked checkboxes. This code starts at line 262 for me. I am using Uniform version 1.7.5. The lines between the 'added by John' notes are what I added.
我知道这可能有点晚了,但希望它会帮助某人。我不得不在统一代码中添加几行,以便它查找预先选中的复选框。对我来说,此代码从第 262 行开始。我正在使用统一版本 1.7.5。“由约翰添加”注释之间的线条是我添加的内容。
"click.uniform touchend.uniform": function(){
if(!$(elem).attr("checked")){
//box was just unchecked, uncheck span
spanTag.removeClass(options.checkedClass);
// Added by John to look for checkboxes with the attr "checked" that have been click to uncheck
}else if(!$(elem).is(':checked')){
spanTag.removeClass(options.checkedClass);
// end addition by john
}else{
//box was just checked, check span.
spanTag.addClass(options.checkedClass);
}
},
回答by Jeff B.
Its been a while but I had the same problem but I resolved it by a simple solution.
已经有一段时间了,但我遇到了同样的问题,但我通过一个简单的解决方案解决了它。
Uniform is adding a span
over the checkbox and gives it the class checked
if it is checked or not, but on page load only. If you check or uncheck the checkbox while the page is already loaded, it will uncheck (or check) but since the span
is over it, you will see it as not updated, you have to do it manually.
Uniformspan
在复选框上添加一个,并在checked
选中或未选中时为其提供类,但仅在页面加载时。如果您在页面已加载时选中或取消选中复选框,它将取消选中(或选中),但由于span
已结束,您将看到它未更新,您必须手动执行此操作。
$('#myDiv input[type=checkbox]').attr('checked',true);
$('#myDiv input[type=checkbox]').parent().addClass('checked');
or
或者
$('#myDiv input[type=checkbox]').attr('checked',false);
$('#myDiv input[type=checkbox]').parent().removeClass('checked');
Might not be the cleanest solution, but works fine on every browser.
可能不是最干净的解决方案,但在每个浏览器上都能正常工作。
回答by Grant
jQuery 1.6.4 changed the meaning of the attr()
function, which is used by jquery.uniform.js. attr()
now returns the state that the attribute WAS in when the page loaded - not what the state of the attribute is NOW. There is a replacement function called prop()
, which does the job that attr()
used to do.
jQuery 1.6.4 更改了attr()
jquery.uniform.js 使用的函数的含义。 attr()
现在返回页面加载时属性所在的状态 - 而不是属性的状态现在。有一个名为 的替换函数prop()
,它可以完成以前的工作attr()
。
To fix the plugin, replace each occurrence of attr("checked")
with prop("checked")
. Also, change attr("disabled")
to prop("disabled")
.
要解决这个插件,替换的每次出现attr("checked")
用prop("checked")
。此外,更改attr("disabled")
为prop("disabled")
.
That should fix your project up. It worked for me ;-)
那应该修复您的项目。它对我有用;-)
See also: https://github.com/pixelmatrix/uniform/pull/167, which attempts to address the issue with better backwards compatibility.
另请参阅:https: //github.com/pixelmatrix/uniform/pull/167,它试图以更好的向后兼容性来解决该问题。
回答by Danny
I believe the latest version of Jquery uses a different method to evaluate a :checked value. Using a slightly older version (although not ideal) fixed this problem for me. That was a while ago though, and PixelMatrix still don't seem to have updated Uniform.
我相信最新版本的 Jquery 使用不同的方法来评估 :checked 值。使用稍旧的版本(虽然不理想)为我解决了这个问题。不过那是很久以前的事了,PixelMatrix 似乎还没有更新 Uniform。
回答by Mads Ohm Larsen
Couldn't you use something like
你不能用类似的东西吗
$("#checkbox").attr("checked", "true");
to make the checkbox checked, and
选中复选框,以及
$("#checkbox").removeAttr("checked");
to uncheck it?
取消选中它?
回答by PingOn
You don't need to change .attr()
to .prop()
:
您无需更改.attr()
为.prop()
:
"click.uniform touchend.uniform": function(){
if(!$(elem).attr('checked')){
//box was just unchecked, uncheck span
spanTag.removeClass(options.checkedClass);
//Added by PingOn
$(elem).removeAttr('checked');
//end addition by PingOn
// Added by John
}else if(!$(elem).is(':checked')){
spanTag.removeClass(options.checkedClass);
// end addition by john
//Added by PingOn
$(elem).removeAttr('checked');
//end addition by PingOn
}else{
//box was just checked, check span.
spanTag.addClass(options.checkedClass);
//Added by PingOn
$(elem).attr('checked','true');
//end addition by PingOn
}