简体   繁体   English

动态验证所有无线电输入是否被检查

[英]Dynamically validate all radio inputs are checked

I've got many inputs in a table. 我在表中有很多输入。 I want to validate each time i get in the page that if all the radioOpciones* have one option checked. 我想每次进入页面时都验证一下是否所有radioOpciones*都选中了一个选项。 If it's true, I will show() something. 如果是真的,我将show()一些东西。

 <table> <tr> <th> title </th> <td class="radioGroup"> <input id="radioOpciones1" value="1" name="radioOpciones1" checked="checked" type="radio"> </td> <td class="radioGroup"> <input id="radioOpciones2" value="2" name="radioOpciones1" type="radio"> </td> <td class="radioGroup"> <input id="radioOpciones3" value="3" name="radioOpciones1" type="radio"> </td> <td class="radioGroup"> <input id="radioOpciones4" value="4" name="radioOpciones1" type="radio"> </td> <td class="radioGroup"> <input id="radioOpciones5" value="5" name="radioOpciones1" type="radio"> </td> </tr> <tr> <th> title2 </th> <td class="radioGroup"> <input id="radioOpciones6" value="1" name="radioOpciones2" checked="checked" type="radio"> </td> <td class="radioGroup"> <input id="radioOpciones7" value="2" name="radioOpciones2" type="radio"> </td> <td class="radioGroup"> <input id="radioOpciones8" value="3" name="radioOpciones2" type="radio"> </td> <td class="radioGroup"> <input id="radioOpciones9" value="4" name="radioOpciones2" type="radio"> </td> <td class="radioGroup"> <input id="radioOpciones10" value="5" name="radioOpciones2" type="radio"> </td> </tr> </table> 

I was trying to get the length by 我试图通过长度

$('.radioGroup').length

and then compare with checked options 然后与选中的选项进行比较

$('.radioGroup:has(input:checked)').length

I want to get when i click in each radio that if it's the last radio option checked possible to send a continue button. 当我单击每个单选按钮时,如果要检查最后一个单选按钮,则可以发送继续按钮。

UPDATED 更新

Search for distinct radio input names, pass them to checkRadioGroups() function and get results. 搜索不同的无线电输入名称,将其传递给checkRadioGroups()函数并获取结果。

You can use this function each time you want to check the checked state of the radio inputs. 每当您要检查无线电输入的checked状态时,都可以使用此功能。

NOTE 注意

You can use this approach in any possible HTML structure. 您可以在任何可能的HTML结构中使用此方法。

 function checkRadioGroups(collection) { var obj = {}; for (var i in collection) { var nameAttr = collection[i]; var isChecked = $('input[name="' + nameAttr + '"]:checked').length > 0; obj[nameAttr] = isChecked; } return obj; } function getDistinctGroupNames() { var names = []; $('input[type="radio"]').each(function(index, elem) { var name = $(elem).attr('name'); if (names.indexOf(name) === -1) { names.push(name); } }); return names; } var check = checkRadioGroups(getDistinctGroupNames()); console.log(check); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <th> title </th> <td class="radioGroup"> <input id="radioOpciones1" value="1" name="radioOpciones1" checked="checked" type="radio"> </td> <td class="radioGroup"> <input id="radioOpciones1" value="2" name="radioOpciones1" type="radio"> </td> <td class="radioGroup"> <input id="radioOpciones1" value="3" name="radioOpciones1" type="radio"> </td> <td class="radioGroup"> <input id="radioOpciones1" value="4" name="radioOpciones1" type="radio"> </td> <td class="radioGroup"> <input id="radioOpciones1" value="5" name="radioOpciones1" type="radio"> </td> </tr> <tr> <th> title2 </th> <td class="radioGroup"> <input id="radioOpciones2" value="1" name="radioOpciones2" type="radio"> </td> <td class="radioGroup"> <input id="radioOpciones2" value="2" name="radioOpciones2" type="radio"> </td> <td class="radioGroup"> <input id="radioOpciones2" value="3" name="radioOpciones2" type="radio"> </td> <td class="radioGroup"> <input id="radioOpciones2" value="4" name="radioOpciones2" type="radio"> </td> <td class="radioGroup"> <input id="radioOpciones2" value="5" name="radioOpciones2" type="radio"> </td> </tr> </table> 

try this code 试试这个代码

$(function() {

    var radios = $('input[type=radio][name*=radioOpciones]').filter(function() {
        return $(this).is(':checked')
    });
    alert(radios.length);

});

hope this helps 希望这可以帮助

This is how I'd address this using a <radio-group> webcomponent. 这就是我使用<radio-group> webcomponent解决此问题的方式。 The element represents the missing radio-group element which you can simply ask for the value. 该元素表示缺少的radio-group元素,您可以简单地要求该值。 null means none of the radio buttons is :checked . null表示没有:checked任何单选按钮。 Otherwise, the radio-group element reports the value of the :checked radiobutton: 否则,单选组元素报告:checked单选按钮的值:

 class RadioGroup extends HTMLElement { constructor(...args) { const self = super(...args) self.type = 'radio-group' self.radioButtons = null return self } get value() { const checked = this.querySelector(':checked') return checked ? checked.value : null } set value(val) { const radios = this.radioButtons.filter(i => i.value === val) if (radios.length) { radios[0].checked = true } else { for (const radioButton of this.radioButtons) { radioButton.checked = false } } let change = createNewEvent('change', true, true) this.dispatchEvent(change) } connectedCallback() { if (this.nextSibling) { // children parsed? this.radioButtons = [...this.querySelectorAll('input[type=radio]')] } else { // if not, populate radioButtons only on next animation frame window.requestAnimationFrame(() => { this.radioButtons = [...this.querySelectorAll('input[type=radio]')] }) } } } window.customElements.define('radio-group', RadioGroup) let radioGroups = Array.from(document.querySelectorAll('radio-group')) check.addEventListener('click', (e) => { console.log(radioGroups.every(radioGroup => radioGroup.value)) }) 
 <radio-group id="sex"> <fieldset> <legend>Sex</legend> <input type="radio" value="female" id="female" name="sex" /> <label for="female">female</label> <input type="radio" value="male" id="male" name="sex" /> <label for="male">male</label> <input type="radio" value="diverse" id="diverse" name="sex" /> <label for="diverse">diverse</label> </fieldset> </radio-group> <radio-group id="color"> <fieldset> <legend>Color</legend> <input type="radio" value="blue" id="blue" name="color" /> <label for="blue">blue</label> <input type="radio" value="red" id="red" name="color" /> <label for="red">red</label> <input type="radio" value="green" id="green" name="color" /> <label for="green">green</label> </fieldset> </radio-group> <button id="check" type="button">Check if each radiogroup has a checked radiobutton</button> 

Firstly, you need to give different id's to your inputs in same page cause of HTML rules. 首先,由于HTML规则,您需要为同一页面中的输入提供不同的ID。 Because id specifies a unique id for the element. 因为id为元素指定了唯一的ID。 Here 这里

And your elegant solution is here : 您的优雅解决方案在这里:

if($('input.classname').not(':checked').length > 0) {
  ...
}

OR 要么

 if($('input[name="inputname"]').not(':checked').length > 0) {
  ...
}

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

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