简体   繁体   English

Javascript:当使用同一类检查其他 15 个单选按钮时,如何启用单选按钮?

[英]Javascript: How can I enable a radio button when 15 other radio buttons are checked with the same class?

I am trying to enable a radio button after 15 other radio buttons (code snippet below) are clicked.我试图在单击其他 15 个单选按钮(下面的代码片段)后启用单选按钮。 How can I do this in pure JS (no jQuery)?我怎样才能在纯 JS(没有 jQuery)中做到这一点?

<div class="assessment-question">
  <span><em>{% trans "I do more than what's expected of me" %}</em></span>
  <ul class="assessment-answer-list">
    <li>
      <input type="radio" name="question11" value="0-0" class="assess_answers" required> {% trans 'Never' %}
    </li>
    <li>
      <input type="radio" name="question11" value="1-0" class="assess_answers"> {% trans 'Rarely' %}
    </li>
    <li>
      <input type="radio" name="question11" value="2-1" class="assess_answers"> {% trans 'Sometimes' %}
    </li>
    <li>
      <input type="radio" name="question11" value="3-1" class="assess_answers"> {% trans 'Often' %}
    </li>
    <li>
      <input type="radio" name="question11" value="4-1" class="assess_answers"> {% trans 'Always' %}
    </li>
  </ul>
</div>
<div class="assessment-question">
  <span><em>{% trans 'I feel comfortable around people' %}</em></span>
  <ul class="assessment-answer-list">
    <li>
      <input type="radio" name="question12" value="0-0" class="assess_answers" required> {% trans 'Never' %}
    </li>
    <li>
      <input type="radio" name="question12" value="1-0" class="assess_answers"> {% trans 'Rarely' %}
    </li>
    <li>
      <input type="radio" name="question12" value="2-1" class="assess_answers"> {% trans 'Sometimes' %}
    </li>
    <li>
      <input type="radio" name="question12" value="3-1" class="assess_answers"> {% trans 'Often' %}
    </li>
    <li>
      <input type="radio" name="question12" value="4-1" class="assess_answers"> {% trans 'Always' %}
    </li>
  </ul>
</div>

I currently have a "next" radio button that is disabled and I want to enable it after all 15 of the above (only a few shown) are checked.我目前有一个禁用的“下一步”单选按钮,我想在检查上述所有 15 个(仅显示几个)后启用它。 They all have the same class name so I can identify them under the same umbrella.它们都有相同的类名,所以我可以在同一把伞下识别它们。

I have tried a couple of solutions that have not worked.我尝试了几个没有奏效的解决方案。 The solutions I have tried all seem to need to have a function activated when each radio button is checked.我尝试过的所有解决方案似乎都需要在选中每个单选按钮时激活一个功能。

Can I use addEventListener or something?我可以使用addEventListener吗?

just use the document.getElementsByClassName to get them all, keep track of the values (AKA, check if all of the values are true with a function), add a few eventlisteners for when it is checked, then once its clicked etc, then check them all to see if the value of all is true.只需使用 document.getElementsByClassName 来获取它们,跟踪这些值(AKA,用函数检查所有值是否为真),在检查时添加一些事件侦听器,然后单击等,然后检查它们全部以查看所有值是否为真。 For example:例如:

var checkedBoxes = Array.apply(null,document.getElementsByClassName("myClassName"));
checkedBoxes.forEach(x => {
    x.addEventListener("click", onActive);
    x.addEventListener("focus", onActive);
    x.addEventListener("blur", onActive);
    //other event listeeners
});
function checkAll() {
     return !checkedBoxes.find(x => !x.checked) //can't find any radio button whose value is false, which means they are all true, which means they are all checked
}
function onActive(e) {
    if(checkAll()) {
        //activate next button
    }
}

Look at this real snippet to see how to do看看这个真实的片段,看看怎么做

 const myForm = document.getElementById('my-form') , gRnames = [...new Set([...myForm.elements].filter(n=>(n.type==='radio')).map(n=>n.name))] ; function SetButtonNextOnOff() { let answers = gRnames.reduce((tot,el)=>tot+=myForm[el].value===''?0:1,0) myForm.bt_next.disabled = (answers!=gRnames.length) } SetButtonNextOnOff() ; myForm.oninput = SetButtonNextOnOff ; myForm.onsubmit =e=> { e.preventDefault() console.clear() Array.from(new FormData(myForm),e=>console.log(e[0],'->',e[1])) myForm.reset() } myForm.onreset =_=>{ myForm.bt_next.disabled = true } ;
 label { display:block; } fieldset { margin:1em; width:23em; } .as-console-wrapper { max-height: 100% !important; width: 30% !important; top: 0; left: 70% !important; }
 <form action="xxx" id="my-form" > <fieldset> <legend>I do more than what's expected of me </legend> <label><input type="radio" name="question11" value="0-0"> Never </label> <label><input type="radio" name="question11" value="1-0"> Rarely </label> <label><input type="radio" name="question11" value="2-1"> Sometimes </label> <label><input type="radio" name="question11" value="3-1"> Often </label> <label><input type="radio" name="question11" value="4-1"> Always </label> </fieldset> <fieldset> <legend>I feel comfortable around people</legend> <label><input type="radio" name="question12" value="0-0"> Never </label> <label><input type="radio" name="question12" value="1-0"> Rarely </label> <label><input type="radio" name="question12" value="2-1"> Sometimes </label> <label><input type="radio" name="question12" value="3-1"> Often </label> <label><input type="radio" name="question12" value="4-1"> Always </label> </fieldset> <button name="bt_next" type="submit">Next</button> <button type="reset">reset</button> </form>

If you want all 15 radio buttons to be checked, their name property should be different than one another如果您希望选中所有 15 个单选按钮,它们的 name 属性应该彼此不同

Each radio element should have click/change event and function defined for the event to identify whether it's clicked or not每个单选元素都应该有点击/更改事件和为事件定义的函数来识别它是否被点击

Each time the radio button clicked, check whether it's enabled / disabled and update counter for the required no of radio buttons clicked accordingly.每次单击单选按钮时,检查它是否已启用/禁用,并相应地更新所需单选按钮数量的计数器。

Compare it with the no of radio buttons with the same class(getElementsByClassName())将它与具有相同类的单选按钮的数量进行比较(getElementsByClassName())

if count is same means all required radio buttons are enabled, you can manually enable the next radio button in the event function如果计数相同意味着启用了所有必需的单选按钮,您可以手动启用事件功能中的下一个单选按钮

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

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