简体   繁体   English

单选按钮输入的`checked`属性不存在

[英]`checked` attribute on radio button input does not exist

When trying to create an radio button input and setting the checked attribute, the checked attribute never appears. 当尝试创建单选按钮输入并设置checked属性时, checked属性将永远不会出现。 Interestingly, the CSS for :checked state elements works as does VoiceOver read out for the selected radio button at the time. 有趣的是, :checked状态元素的CSS就像VoiceOver当时为选定的单选按钮读出的那样工作。

Here's an Ember twiddle that displays this issue: 这是一个显示该问题的Ember玩意儿:

https://ember-twiddle.com/1c3c6686c49a0c8338e24acc66b05a26 https://ember-twiddle.com/1c3c6686c49a0c8338e24acc66b05a26

You'll see that the checked attribute never appears on the selected radio button, but both the DOM element and CSS display as checked or selected. 您将看到, checked属性从不出现在选中的单选按钮上,但是DOM元素和CSS都显示为选中或选中状态。

Does anyone know what might be causing this apparent conflict? 有谁知道是什么原因导致这种明显的冲突?

The problem is that you didn't even set value attribute to the radio buttons and also didn't change the current property in the controller/application.js . 问题在于您甚至没有将value属性设置为单选按钮,也没有更改controller/application.jscurrent属性。

You should change three things in the templates/application.hbs . 您应该在templates/application.hbs更改三件事。 First you should include value attribute to the radio buttons value={{option}} and an action on the onclick attribute onclick={{action 'changeChecked'}} to specify the change. 首先,您应该将value属性包括在单选按钮value={{option}}并在onclick属性上执行一个动作onclick={{action 'changeChecked'}}以指定更改。 Since the {{eq}} helper itself returns true or false you can also write the checked attribute without the {{if}} helper as checked={{eq option current}} The final input tag should look like : 由于{{eq}}辅助函数本身返回true or false您也可以在没有{{if}}辅助checked={{eq option current}}情况下,将checked属性写为checked={{eq option current}} ,最终输入标签应如下所示:

<input type="radio" name="colors" checked={{eq option current}} value={{option}} onclick={{action 'changeChecked'}}>

After that, in the controllers/application.js you have to add the action changeChecked() 之后,必须在controllers/application.js中添加动作changeChecked()

actions:{
  changeChecked(){
    set(this,'current',$('input:checked').val())
  }
},

And also, the return statement in the whichChecked should be changed to : 而且, whichChecked的return语句应更改为:

return $('input:radio:checked').val();

Now you can dynamically get the selected radio button in the DOM. 现在,您可以在DOM中动态获取选定的单选按钮。

You should be using: 您应该使用:

$('input:radio:checked').val()

instead of: 代替:

$('input[checked]').val()

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

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