[英]How do I check to make sure all my input:radio elements have a value?
I'm working on a form, and I've got a code that works for validating radio fields. 我正在处理表单,并且有一个用于验证无线电字段的代码。 However I plan on having many, MANY radio fields and I feel that my current method, which names each radio field individually, to be a bit tedious. 但是,我计划有许多个无线电字段,并且我觉得当前的方法(分别命名每个无线电字段)有点乏味。 I feel there must be a way I can simply select all of them to make sure they've all been checked before processing the form. 我觉得必须有一种方法可以简单地选择所有它们,以确保在处理表单之前已对所有它们进行了检查。
Here is my jsFiddle 这是我的jsFiddle
Current jQuery 当前的jQuery
$("form").on("keyup change", function(){
food = $(":checked[name=food]");
color = $(":checked[name=color]");
if (food.val() == "" || food.val() == undefined ||
color.val() == "" || color.val() == undefined){
//Do stuff
} else {
//Do other stuff
}
});
jQuery I'm trying 我正在尝试的jQuery
$("form").on("keyup change", function(){
radio_req = $(".required:checked[type=radio]");
if ( radio_req == "" || radio_req == undefined ) {
//Do stuff
} else {
//Do other stuff
}
});
With the 2nd one, I'm trying to see if I can just add a class of required to all my radio fields, and then just see if any .required
radio buttons are left unselected/undefined. 对于第二个,我试图查看是否可以向所有单选字段添加必填类,然后查看是否未选中/未定义任何.required
单选按钮。 I feel there has to be a way for this to work. 我觉得必须有一种方法可以解决这个问题。 Am I on the right track? 我在正确的轨道上吗? I feel like I'm just not selecting this right. 我觉得我只是没有选择这个权利。
I have updated your fiddle , check it ! 我已经更新了您的小提琴 ,请检查! I hope it will solve your problem. 希望它能解决您的问题。
You can test the snippet under. 您可以在下面测试代码段。 This is the part i modified : 这是我修改的部分:
var checker = function(){
var checked = $.unique($(".required:checked[type=radio]").map(function(){
return this.name;
}));
var not_checked = $.unique($(".required:not(:checked)[type=radio]").map(function(){
return this.name;
}));
if (checked.length !== not_checked.length){
$("#submit").prop("disabled", true).removeClass("valid").addClass("invalid");
$(".submit").text("* All fields must be completed and contain valid entries before submitting.");
} else {
$("#submit").prop("disabled", false).removeClass("invalid").addClass("valid");
$(".submit").text("");
}
};
$("form").on("keyup change", checker);
checker();
$(document).ready(function(){ // for each Question we have 3 choices: // so when we answer we have 1 checked and 2 unchecked // we loop over question // we fill 2 arrays(checked, unchecked) // with names of radio // for each question we will have something like that // Question 1 : food // checked = ['food']; notchecked = ['food','food'] // we make them unique ! it becomes // checked = ['food']; notchecked = ['food']; // we know now that the First question have been answered // we loop over all question : // checked = ['food'] , notchecked = ['food' , 'color' , 'town' , 'country' , 'car'] // we wait for checking ! // let answer Qestion 2 // checked = ['food' , 'color] , notchecked = ['food' , 'color' , 'town' , 'country' , 'car'] // etc etc . . . // when checked.length === notchecked.length => we have an answer for all question var checker = function(){ var checked = $.unique( $(".required:checked[type=radio]").map(function(){ return this.name; }) ); var not_checked = $.unique( $(".required:not(:checked)[type=radio]").map(function(){ return this.name; }) ); if (checked.length !== not_checked.length){ $("#submit").prop("disabled", true).removeClass("valid").addClass("invalid"); $(".submit").text("* All fields must be completed and contain valid entries before submitting."); } else { $("#submit").prop("disabled", false).removeClass("invalid").addClass("valid"); $(".submit").text(""); } }; $("form").on("keyup change", checker); checker(); });
input { margin: 7px; } .valid { background-color: green; border: 3px solid darkgreen; color: white; } .invalid { background-color: grey; border: 3px solid darkgrey; color: black; } .error { color: red; } #submit { display: block; border-radius: 10px 10px 10px 10px; width: 130px; margin: auto; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <br>Which of the following do you like the most? <br> <input type="radio" class="required" name="food" value="pizza">Pizza <input type="radio" class="required" name="food" value="burgers">Burgers <input type="radio" class="required" name="food" value="Salads">Salads <br> <br>Which of the following colors do you like the most? <br> <input type="radio" class="required" name="color" value="red">Red <input type="radio" class="required" name="color" value="blue">Blue <input type="radio" class="required" name="color" value="yellow">Yellow <br> <br>Which of the following town do you like the most? <br> <input type="radio" class="required" name="town" value="red">New York <input type="radio" class="required" name="town" value="blue">Miami <input type="radio" class="required" name="town" value="yellow">Las Vegas <br> <br>Which of the following country do you like the most? <br> <input type="radio" class="required" name="country" value="red">USA <input type="radio" class="required" name="country" value="blue">Canada <input type="radio" class="required" name="country" value="yellow">Chili <br> <br>Which of the following car do you like the most? <br> <input type="radio" class="required" name="car" value="red">Ferrari <input type="radio" class="required" name="car" value="blue">Dodge <input type="radio" class="required" name="car" value="yellow">Chevrolet <br><br> <input type="submit" id="submit" class="invalid"> <span class="submit error"></span> </form>
You can iterate through all the radio buttons that have a name attribute :radio[name]
in combination with an array, say examined
, that helps you skip buttons that have already been checked. 您可以遍历所有具有名称属性:radio[name]
的单选按钮,并结合一个数组(例如, examined
,以帮助您跳过已经检查的按钮。 This should be work irrespective of how many sets of radio buttons there are in you form. 无论表单中有多少组单选按钮,此方法都应该起作用。
$('form').on('input change', function(e) {
e.preventDefault();
var all_selected = true;
var examined = [];
$(':radio[name]').each(function() {
if( examined.indexOf( this.name ) === -1 ) {
examined.push( this.name );
if( !$(':radio:checked[name=' + this.name + ']').length ) {
all_selected = false;
return false;
}
}
});
if( all_selected ) {
//DO STUFF
} else {
//DO OTHER STUFF
}
});
$(function() { $('form').on('input change', function(e) { e.preventDefault(); var all_selected = true; var examined = []; $(':radio[name]').each(function() { if( examined.indexOf( this.name ) === -1 ) { examined.push( this.name ); if( !$(':radio:checked[name=' + this.name + ']').length ) { all_selected = false; return false; } } }); if( all_selected ) { //DO STUFF console.log( 'All parts completed' ); } else { //DO OTHER STUFF console.log( 'Form not complete' ); } }); });
input { margin: 7px; } .valid { background-color: green; border: 3px solid darkgreen; color: white; } .invalid { background-color: grey; border: 3px solid darkgrey; color: black; } .error { color: red; } #submit { display: block; border-radius: 10px 10px 10px 10px; width: 130px; margin: auto; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> Which of the following do you like the most?<br> <input type="radio" class="required" name="food" value="pizza">Pizza <input type="radio" class="required" name="food" value="burgers">Burgers <input type="radio" class="required" name="food" value="Salads">Salads <br><br> Which of the following colors do you like the most?<br> <input type="radio" class="required" name="color" value="red">Red <input type="radio" class="required" name="color" value="blue">Blue <input type="radio" class="required" name="color" value="yellow">Yellow <input type="submit" id="submit" class="invalid"> <span class="submit error"></span> </form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.