简体   繁体   English

选择所有集合中的单选按钮时如何启用按钮?

[英]How to enable a button when radio button from all the sets are selected?

I am working on a test and bumped into a problem.我正在做一个测试并遇到了一个问题。

How do I make a .js script that only enables button when all fields are checked.如何制作仅在选中所有字段时启用按钮的 .js 脚本。

<tr>
    <td class="first-col">Ritkán szorongom1
        <td><input type="radio" name="q1" value="1" id="q1"></td>
        <td><input type="radio" name="q1" value="2" id="q1"></td>
        <td><input type="radio" name="q1" value="3" id="q1"></td>
        <td><input type="radio" name="q1" value="4" id="q1"></td>
        <td><input type="radio" name="q1" value="5" id="q1"></td>
    </td>
</tr>
<tr>
    <td class="first-col">Ritkán szorongom1
        <td><input type="radio" name="q2" value="1" id="q2"></td>
        <td><input type="radio" name="q2" value="2" id="q2"></td>
        <td><input type="radio" name="q2" value="3" id="q2"></td>
        <td><input type="radio" name="q2" value="4" id="q2"></td>
        <td><input type="radio" name="q2" value="5" id="q2"></td>
    </td>
</tr>
<tr>
    <td class="first-col">Ritkán szorongom1
        <td><input type="radio" name="q3" value="1" id="q3"></td>
        <td><input type="radio" name="q3" value="2" id="q3"></td>
        <td><input type="radio" name="q3" value="3" id="q3"></td>
        <td><input type="radio" name="q3" value="4" id="q3"></td>
        <td><input type="radio" name="q3" value="5" id="q3"></td>
    </td>
</tr>

<div class="register-button">
    <input type="submit" name="submit1" class="inputButton" id="submit1" value="Következő" disabled id="enable-on-two" />
</div>

I have found this .js online, but I can't make it fit well as I have multiple pages of test questions with different input names.我在网上找到了这个 .js,但我不能很好地适应它,因为我有多页具有不同输入名称的测试题。

function numberOfCheckboxesSelected() {
    return document.querySelectorAll('input[type=checkbox][name="seatdata[]"]:checked').length;
}

function onChange() {
    document.getElementById('enable-on-two').disabled = numberOfCheckboxesSelected() < 3;
}

document.getElementById('world').addEventListener('change', onChange, false);

There were some issues with your codebase.您的代码库存在一些问题。

  1. document.getElementById('world').addEventListener('change', onChange, false);

    Issue There is no node with that specified id.问题没有具有该指定 ID 的节点。

  2. Submit button has more than one id.提交按钮有多个 ID。 That is not possible.这是不可能的。

  3. document.querySelectorAll('input[type=checkbox][name="seatdata[]"]:checked')

Your input type is radio so the above one wont work.您的输入类型是radio因此上述输入类型不起作用。 Also the name attribute is wrong. name 属性也是错误的。

I have added those fixes.我已经添加了这些修复。

 function numberOfCheckboxesSelected() { return document.querySelectorAll('input[type=radio]:checked').length; } function onChange() { console.log(numberOfCheckboxesSelected()); document.getElementById('enable-on-two').disabled = numberOfCheckboxesSelected() < 3; } document.querySelectorAll('input[type=radio]').forEach(node => { node.addEventListener('change', onChange, false); });
 <table> <tr> <td class="first-col">Ritkán szorongom1 <td><input type="radio" name="q1" value="1" id="q1"></td> <td><input type="radio" name="q1" value="2" id="q1"></td> <td><input type="radio" name="q1" value="3" id="q1"></td> <td><input type="radio" name="q1" value="4" id="q1"></td> <td><input type="radio" name="q1" value="5" id="q1"></td> </td> </tr> <tr> <td class="first-col">Ritkán szorongom1 <td><input type="radio" name="q2" value="1" id="q2"></td> <td><input type="radio" name="q2" value="2" id="q2"></td> <td><input type="radio" name="q2" value="3" id="q2"></td> <td><input type="radio" name="q2" value="4" id="q2"></td> <td><input type="radio" name="q2" value="5" id="q2"></td> </td> </tr> <tr> <td class="first-col">Ritkán szorongom1 <td><input type="radio" name="q3" value="1" id="q3"></td> <td><input type="radio" name="q3" value="2" id="q3"></td> <td><input type="radio" name="q3" value="3" id="q3"></td> <td><input type="radio" name="q3" value="4" id="q3"></td> <td><input type="radio" name="q3" value="5" id="q3"></td> </td> </tr> </table> <div class="register-button"> <input type="submit" name="submit1" class="inputButton" value="Következő" disabled id="enable-on-two" /> </div>

The above solution needs a little correction as it enables the submit button on two inputs selection instead of all three.上述解决方案需要稍加修正,因为它启用了两个输入选择上的提交按钮,而不是所有三个。

Replace following:替换以下内容:

document.getElementById('enable-on-two').disabled = numberOfCheckboxesSelected() < 2;

By this:这样:

let totalInputs = document.querySelectorAll('input[type=radio]').length;
document.getElementById('enable-on-two').disabled = numberOfCheckboxesSelected() <   totalInputs;

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

相关问题 选择单选按钮时启用单击按钮 - Enable click button when a radio button is selected 如果选中单选按钮,则启用按钮 - Enable button if radio button selected 从单选按钮中选择特定日期时启用禁用设置单选按钮 - Enable disable set radio button when particular date is selected from radio button Ionic 3 Dynamic单选按钮formBuilder验证(如果全部选中则启用按钮) - Ionic 3 Dynamic radio button formBuilder validation if all selected then enable button 仅当所有问题中的单选按钮都被选中时才启用提交按钮 jquery - enable submit button only if radio button in all questions are selected jquery 选择某些单选按钮时切换显示并启用文本文件 - Toggle Show and Enable Textfiled When Certain Radio Button is Selected 仅在同时选择两个无线电组时启用按钮 - Enable button only when both radio groups are selected 如果id和name相同,如何启用/禁用一个单选按钮(如果选择了另一个按钮) - how to enable / disable one radio button if another is selected when id and name are same 选择某些复选框(单选按钮)时如何启用/禁用 X 输入? - How to enable/disable X inputs when certain checkbox (radio button) is selected? 选择单选按钮时如何隐藏日期选择器? - How to hide datepicker when radio button is selected?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM