jqueryvalidate-文本输入不断清除自身
如果复选框未选中且文本框为空,则需要弹出验证消息。它确实会弹出,但会在输入时删除文本,并将其保留为无效。如何使验证在键入第一个字符时消失,并且除非文本框为空,否则不会重新出现jqueryvalidate-文本输入不断清除自身,jquery,textbox,jquery-validate,Jquery,Textbox,Jquery Validate,如果复选框未选中且文本框为空,则需要弹出验证消息。它确实会弹出,但会在输入时删除文本,并将其保留为无效。如何使验证在键入第一个字符时消失,并且除非文本框为空,否则不会重新出现 <form id="practiceForm"> <input type="text" name="firstName" id="textbox"/> <input type="checkbox" name="active" id="checkbox"/> &l
<form id="practiceForm">
<input type="text" name="firstName" id="textbox"/>
<input type="checkbox" name="active" id="checkbox"/>
<input type="submit" id="submit" value="Submit"/>
</form>
<script type="text/javascript">
$('#checkbox').attr('checked', true);
if($('#checkbox').attr('checked')){
$('#textbox').attr('disabled', true);
$('#textbox').val('');
} else {
$('#textbox').attr('disabled', false);
};
$('#checkbox').change(function () {
if($('#checkbox').attr('checked')){
$('#textbox').attr('disabled', true);
$('#textbox').val('');
} else {
$('#textbox').attr('disabled', false);
};
});
$.validator.addMethod("textValidate", function(value){
if(!$('#checkbox').attr('checked')){
if(!$('#textbox').val('') || !$('#textbox').val(null)){
return true;
};
};
}, "If box is not checked then there must be text"
);
$('#practiceForm').validate({
rules: {
//textValidate: true
firstName:{
textValidate: true
}
}
});
</script>
$('#checkbox').attr('checked',true);
if($('#复选框').attr('checked')){
$('#textbox').attr('disabled',true);
$('#文本框').val('');
}否则{
$('#textbox').attr('disabled',false);
};
$('#复选框')。更改(函数(){
if($('#复选框').attr('checked')){
$('#textbox').attr('disabled',true);
$('#文本框').val('');
}否则{
$('#textbox').attr('disabled',false);
};
});
$.validator.addMethod(“textValidate”),函数(值){
if(!$('#复选框').attr('checked')){
if(!$('#textbox').val('')| |!$('#textbox').val(null)){
返回true;
};
};
},“如果未选中此框,则必须有文本”
);
$(“#practiceForm”)。验证({
规则:{
//textValidate:true
名字:{
textValidate:true
}
}
});
正在将#textbox
的值设置为“”,然后设置为null,并使if语句始终返回false
你的意思可能是这样的:
if ($('#textbox').val() != '' || $('#textbox').val() != null) {
textValidate
方法中的此逻辑已中断:
if(!$('#textbox').val('') || !$('#textbox').val(null)){
您没有检查'
或null
的值,而是将其设置为值。由于该方法在每个向上键事件上都被调用,因此在您键入时它会清除输入
请尝试以下方法:
$.validator.addMethod("textValidate", function (value) {
if (!$('#checkbox').attr('checked')) {
if (!$('#textbox').val() == '' || !$('#textbox').val() == null) {
return true;
};
};
}, "If box is not checked then there must be text");
工作演示:
次要问题:
你不需要大部分的代码
$('#checkbox').attr('checked', true);
if($('#checkbox').attr('checked')){
$('#textbox').attr('disabled', true);
$('#textbox').val('');
} else {
$('#textbox').attr('disabled', false);
};
它只在DOM ready上运行一次,因为您将#checkbox
设置为checked
,所以看起来像checked
属性的if/then
条件是完全多余和不必要的
可以更简单地写如下。还更改了attr
,在使用jQuery 1.6+时,它在技术上比attr
更正确
$('#checkbox').prop('checked', true);
$('#textbox').prop('disabled', true);
$('#textbox').val('');
这是全面的吗?是否有理由使用attr?@user1795787,而不是“全面”。请参见实际示例中实际使用的复选框。Quote:“属性通常会影响DOM元素的动态状态,而不会更改序列化HTML属性。示例包括输入元素的值
属性、输入和按钮的禁用
属性或复选框的选中
属性。.prop()
方法应用于设置禁用的和选中的而不是.attr()
方法。谢谢。这是一个很大的帮助。当你还不熟悉这一点的时候,很难把一切都弄清楚。谢谢@Sparky,我遇到了同样的错误,这对我帮助很大!
$('#checkbox').prop('checked', true);
$('#textbox').prop('disabled', true);
$('#textbox').val('');