I want to validate the radio buttons in a form with jQuery, but it doesn't work. Even if I check a radio button the code below (variable "checked") returns false.
我想用jQuery验证表单中的单选按钮,但它不起作用。即使我检查单选按钮,下面的代码(变量“checked”)也会返回false。
What am I doing wrong?
我究竟做错了什么?
$('.radio').each(function(){
var checked = $(this).is(':checked');
if($(this).is(':checked') === false){
e.preventDefault();
}
});
3 个解决方案
#1
1
try this
$('.radio').each(function(){
var checked = $(this).attr("checked") == "checked";
if(checked){
e.preventDefault();
}
});
#2
0
use
$('.radio').click(function(){
if($(this).is(':checked') == false){
e.preventDefault();
}
});
use $(this).is(:checked)
or $(this).attr('checked')
to get the status of checkbox
使用$(this).is(:checked)或$(this).attr('checked')来获取复选框的状态
#3
0
$('.radio').each(function(e){
var checked = this.checked; // using this.checked is better
if(!checked){ // not checked
e.preventDefault();
}
});
or if you want to :is(':checked')
then
或者如果你想:is(':checked')那么
$('.radio').each(function(e){
var checked = $(this).is(':checked');
if(!checked){ // not checked
e.preventDefault();
}
});
#1
1
try this
$('.radio').each(function(){
var checked = $(this).attr("checked") == "checked";
if(checked){
e.preventDefault();
}
});
#2
0
use
$('.radio').click(function(){
if($(this).is(':checked') == false){
e.preventDefault();
}
});
use $(this).is(:checked)
or $(this).attr('checked')
to get the status of checkbox
使用$(this).is(:checked)或$(this).attr('checked')来获取复选框的状态
#3
0
$('.radio').each(function(e){
var checked = this.checked; // using this.checked is better
if(!checked){ // not checked
e.preventDefault();
}
});
or if you want to :is(':checked')
then
或者如果你想:is(':checked')那么
$('.radio').each(function(e){
var checked = $(this).is(':checked');
if(!checked){ // not checked
e.preventDefault();
}
});