单选按钮“已选中”属性不起作用
默认情况下,单选按钮不会显示为已选中
。我一开始没有默认选择,做了一些非常简单的 js 验证,但它不起作用。所以我选择只使用默认值,直到我弄清楚并发现发生了一些奇怪的事情。
标记是有效的,我已经在 FF、Safari 和 Chrome 中尝试过。什么都不起作用。
我认为这与 jQuery 库有冲突,因为当我删除调用脚本时问题就消失了。
<label>Do you want to accept American Express?</label> Yes
<input id="amex" style="width: 20px;" type='radio' name='Contact0_AmericanExpress' value='1' /> No
<input style="width: 20px;" type='radio' name='Contact0_AmericanExpress' class='check' value='0' checked="checked" />
The radio button does not show up as checked
by default. I started off without a default choice doing some very simple js validation and it wasn't working. So I opted to just use default values until I figured that out and discovered that something weird is going on.
The markup is valid and I've tried in FF, Safari and Chrome. Nothing works.
I think it's a conflict with the jQuery
library because the problem goes away when I remove the call script.
<label>Do you want to accept American Express?</label> Yes
<input id="amex" style="width: 20px;" type='radio' name='Contact0_AmericanExpress' value='1' /> No
<input style="width: 20px;" type='radio' name='Contact0_AmericanExpress' class='check' value='0' checked="checked" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
嘿,我在ajax生成的页面中也遇到了类似的问题。我使用FF中的Webdeveloper插件生成了源代码,并检查了表单中的所有输入,发现隐藏的div内还有另一个复选框(显示:无)使用相同的ID,一旦我更改了第二个复选框的ID,它就开始工作..你也可以尝试..并让我知道结果..干杯
Hey I was also facing similar problem, in an ajax generated page.. I took generated source using Webdeveloper pluggin in FF, and checked all the inputs in the form and found out that there was another checkbox inside a hidden div(display:none) with same ID, Once I changed the id of second checkbox, it started working.. You can also try that.. and let me know the result.. cheers
只需将您的代码复制到: http://jsfiddle.net/fY4F9/
默认情况下选中“否”。您运行的 JavaScript 是否会影响单选框?
Just copied your code into: http://jsfiddle.net/fY4F9/
No is checked by default. Do you have any javascript running that would effect the radio box?
jQuery 文档提供了针对
checked 属性与属性
。
因此,这是检索选定单选按钮值的另一种方法
The jQuery documentation provides a detailed explanation for
checked
property vs attribute.Accordingly, here is another way to retrieve selected radio button value
我可以通过将两组输入的
input
标记的名称设置为相同的名称来重现此情况,如下所示:(要查看此运行情况,请单击 此处)
以下两种解决方案都可以解决该问题:
form
标签而不是其中一个组的div
标签(无法真正弄清楚这能解决问题的真正原因。很想听听对此的一些意见!)I could repro this by setting the name of
input
tag the same for two groups of input like below:(To see this running, click here)
The following two solutions both fix the problem:
form
tag instead ofdiv
tag for one of the groups (can't really figure out the real reason why this would solve the problem. Would love to hear some opinions on this!)你好,我认为如果你为第二个输入添加 id 属性并给它一个唯一的 id 值,它将起作用
hi I think if you put id attribute for the second input and give it a unique id value it will work
则也尝试这种方式
如果有
also try this way
if there is
<form />
tag then("checked", true)
otherwise("checked", "checked")
您的代码是正确的,请尝试调试您的 JQuery 脚本以查找问题!
如果您使用 FF,您可以安装一个名为 FireBug 的扩展来调试 JS(和 JQuery)。
Your code is right, try to debug your JQuery script to find the issue!
If you're using FF you can install an extension to debug JS (and JQuery) it's called FireBug.
您正在使用非标准 xhtml 代码(值应该用双引号括起来,而不是单引号)
试试这个:
You're using non-standard xhtml code (values should be framed with double quotes, not single quotes)
Try this:
只需向您想要默认检查的每个无线电添加检查属性
试试这个:
just add checked attribute to each radio that you want to have default checked
try this :
将
checked="checked"
替换为checked={true}
。或者您甚至可以将其缩短为“已检查”。这是因为
checked
属性的预期值类型是布尔值。不是字符串。Replace
checked="checked"
withchecked={true}
. Or you could even shorten it to justchecked
.This is because the expected value type of the
checked
prop is a boolean. not a string.如果您有多个具有选中属性的同名单选按钮,它将采用页面上最后选中的单选按钮。
If you have multiple of the same name with the checked attribute it will take the last checked radio on the page.
无线电输入必须位于表单内才能“检查”工作。
Radio inputs must be inside of a form for 'checked' to work.
可能是这样:
是否有jQuery 1.9.1 中的单选按钮有 bug?
简而言之:不要使用 attr() 而是使用 prop() 来检查单选按钮。天啊我讨厌JS...
This might be it:
Is there a bug with radio buttons in jQuery 1.9.1?
In short: Don't use attr() but prop() for checking radio buttons. God I hate JS...
解决这个恼人问题的最终 JavaScript 解决方法 -
只需将 jQuery 命令包装在
setTimeout
中。间隔可以非常小,我使用10
毫秒,看起来效果很好。延迟非常小,最终用户几乎无法察觉。这也适用于
$("#radio-element").trigger('click');
$("#radio-element").attr('checked',true) ;
$("#radio-element").attr('checked',ANYTHING_THAT_IS_NOT_FALSE);
Hacky...hacky...hacky...hacky... 是的,我知道...因此这是一个解决方法...
The ultimate JavaScript workaround to this annoying issue -
Simply wrap the jQuery command in a
setTimeout
. The interval can be extremely small, I use10
milliseconds and it seems to be working great. The delay is so small that it is virtually undetectable to the end users.This will also work with
$("#radio-element").trigger('click');
$("#radio-element").attr('checked',true);
$("#radio-element").attr('checked',ANYTHING_THAT_IS_NOT_FALSE);
Hacky...hacky...hacky...hacky... Yes I know... hence this is a workaround....