函数结果没有显示错误文本
问题描述:
我已经成功地在其他页面上使用过定义。基本上它应该使该字段成为必需字段,但如果仅输入空白字符(默认情况下键入单个空格会导致所需的检查通过),则会失败。我抛出一个警报,只是为了看看处理程序何时被触发以及当时的值为this
。当我期待它时,它就会被解雇,价值就如我所料。它应该返回false
但显然它不是因为错误没有显示。如果我删除了depends
函数,并且只有required: true
,那么当用户离开该字段时,它会正确显示该错误。这是怎么回事?函数结果没有显示错误文本
ContactName: {
required: {
depends: function() {
alert("'" + $(this).val() + "'");
if ($.trim($(this).val()).length === 0) {
$(this).val($.trim($(this).val()));
return false;
}
return true;
}
},
maxlength: 100
}
答
您可以更改规则联系人姓名像(详细看看到rules examples):
ContactName: {
required: true,
minlength: {
depends: function(ele) {
if (ele.value.trim().length === 0) {
ele.value = '';
return false;
}
return true;
}
},
maxlength: 100
}
的片段:
$("#commentForm").validate({
rules: {
ContactName: {
required: true,
minlength: {
depends: function(ele) {
if (ele.value.trim().length === 0) {
ele.value = '';
return false;
}
return true;
}
},
maxlength: 100
}
},
messages: {
ContactName: "Please enter your contact name"
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.16.0/jquery.validate.min.js"></script>
<form class="cmxform" id="commentForm" method="get" action="">
<fieldset>
<p>
<label for="ContactName">Name</label>
<input id="ContactName" name="ContactName" type="text">
</p>
<p>
<input class="submit" type="submit" value="Submit">
</p>
</fieldset>
</form>
所以什么是价值$ .trim($(this).val())。length'? –
@u_mulder 0,我的家伙。 – helrich