如果有一个或多个输入字段为空,如何禁用发送按钮?
我在伪代码中的尝试
if ( $("input:empty") ) {
$("input:disabled")
}
else
// enable the ask_question -button
我一直在阅读这些文章而没有找到正确的解决方案
> Official docs about empty:这就像是不相关的,因为它会查找所有输入字段
> a thread about disabled -feature in jQuery:这似乎是相关的
> to be able to test jQuery Firefox/terminal:获得测试环境对我最有帮助
我现在使用以下代码.
它包含一个关于角色的错误;但是我找不到它.
#1
$(document).ready(function(){
$("#ask_form").validate(){
rules: {
username {
required: true,minlenghth: 2
},email: {
required: true;
minlength: 6
},password {
required: true,minlength: 6
}
}
});
}
#2 Meder的代码
我轻轻地修改了Meder的代码.它会永久禁用send -button,因此它也有bug.
$(document).ready(function(){
var inputs = $('input','#ask_form'),empty = false;
// can also use :input but it will grab textarea/select elements and you need to check for those..
inputs.each(function() {
if ( $(this).val() == '' ) {
empty = true;
return;
}
});
if ( empty ) {
$('.ask_question').attr('disabled','disabled'); // or true I believe.
}
});
解决方法
var $submit = $("input[type=submit]");
if ( $("input:empty").length > 0 ) {
$submit.attr("disabled","disabled");
} else {
$submit.removeAttr("disabled");
}