[英]Radio button validator
How can I validate radio buttons? 如何验证单选按钮? Because this won't work at all.
因为这根本不起作用。 The radio button
name
and ID
is billable
and their value
is either yes
or no
. 单选按钮
name
和ID
是可billable
,它们的value
是yes
或no
。
function formValidator() {
var errors = new Array();
if($("#billable").checked == false) {
errors[0] = "*Billable - Required";
}
if(errors.length > 0) {
var error_msg = 'Please review:';
for(i=0;i<errors.length;i++) {
if(errors[i]!=undefined) {
error_msg = error_msg + "\n" + errors[i];
}
}
alert(error_msg);
return false;
}
return true;
}
Change to 改成
$("#billable:checked").size()
Using $("#billable").checked
you will get undefined
, because this property doesn't exist. 使用
$("#billable").checked
您将得到undefined
,因为此属性不存在。 With $("#billable:checked").size()
you will get how many checked radios do you have (0 or 1) 使用
$("#billable:checked").size()
您将获得有多少个已检查的无线电(0或1)
See in jsfiddle . 在jsfiddle看到 。
if you try sometime with the JQuery validation plugin , all you will need is something like: 如果您尝试使用JQuery验证插件 ,您只需要:
rules: {
'billable[]':{ required:true }
}
as this post , this will help you: 作为这篇文章 ,这将帮助您:
var iz_checked = true;
$('input').each(function(){
iz_checked = iz_checked && $(this).is(':checked');
});
if ( ! iz_checked )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.