Javascript 使用jQuery验证复选框并输入文本值
我需要你的帮助 在启用按钮之前,有没有一种方法可以使用功能强大的jQuery来验证以下条件Javascript 使用jQuery验证复选框并输入文本值,javascript,jquery,html,checkbox,Javascript,Jquery,Html,Checkbox,我需要你的帮助 在启用按钮之前,有没有一种方法可以使用功能强大的jQuery来验证以下条件 如果用户在文本框中输入值,然后选中其中一个复选框,则启用该按钮 如果用户在文本中已经存在一个值,然后选中其中一个复选框,则启用该按钮 从我的角度来看,这将是一个较长的表单字段检查否,如何用jQuery编写 以下是HTML标记: <!DOCTYPE html> <html> <head> </head> <body> <
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<input type="button" value="Add To Calendar" disabled>
<br>
<input type="checkbox" name="dategroup"><input type="text" id="date1">
<br>
<input type="checkbox" name="dategroup"><input type="text" id="date2">
<br>
<input type="checkbox" name="dategroup"><input type="text" id="date3">
</body>
</html>
这可能会让您开始学习。您可以根据需要使字段验证变得复杂或简单
$('input[type=checkbox]')。单击(function(){
var tmp=$(this.next('input').val();
//验证tmp,例如:
如果(tmp.length>1){
//警报(“文本字段有值”);
$('mybutt').prop('disabled',false);
}否则{
//警报(“请在文本字段中提供长值”);
$('mybutt').prop('disabled',true);
$(this.prop('checked',false);
}
});代码>
试试这个方法
$('input').on('change input', function() {
$input = $('input');
$button = $('input[type="button"]');
var arr = [];
$input.each(function() {
if ($(this).attr('type') !== 'button') {
arr.push(check($(this)));
arr.indexOf(false) == -1 ? $button.removeAttr('disabled') : $button.attr('disabled', 'disabled');
}
})
})
function check(elem) {
if ($(elem).attr('type') == 'checkbox' && $(elem).is(':checked')) return true;
if ($(elem).attr('type') == 'text' && $(elem).val().trim().length) return true;
return false;
}
$('input')。在('change input',function()上{
$input=$('input');
$button=$('input[type=“button”]”);
var-arr=[];
$input.each(函数(){
if($(this.attr('type')!='button'){
arr.push(勾选($(此));
arr.indexOf(false)=-1?$button.removeAttr('disabled'):$button.attr('disabled','disabled');
}
})
})
功能检查(elem){
if($(elem).attr('type')='checkbox'&&($(elem).is(':checked'))返回true;
if($(elem).attr('type')=='text'和&$(elem.val().trim().length)返回true;
返回false;
}
是的,jQuery和普通JavaScript都可以做到这一点。你在哪里卡住了,走了多远,出了什么问题,怎么“出了问题”?@mmcrae不,我不认为是这样。。。我在示例中使用该ID,它通常会咧嘴一笑结束。我怀疑是因为我回答了一个OP自己没有尝试过的问题。不过我不在乎,因为我自己也曾去过那里,而且记得很清楚。