我很难让 jQuery 验证插件按我想要的方式工作。
我拥有的是一个包含多个必需输入的表单,规则如下所示:
$("#makeEvent").validate({
onfocusout: false,
rules: {
name: {
required: true,
minLength: 2,
maxLength: 100
},
host: {
required: true,
minLength: 2,
maxLength: 100
},
startDate: {
required: true,
date: true
},
endDate: {
required: true,
date: true
},
desc: {
required: true,
minLength: 10,
maxLength: 255
},
webpage: {
required: false,
url: true
},
email: {
required: true,
email: true
},
}
});
现在我有一个自定义的 .click() 调用,我想在其中验证表单,然后在允许用户发送表单之前向用户显示预览。请看下面的函数:
$('#submitPreview').click(function() {
if($("#makeEvent").valid() === false) {
//What to do if validation fails
} else if($("#makeEvent").valid() === true) {
//Show the preview and let the user either make changes or submit the final result
}
});
但是发生的情况是,该函数仅验证第一个输入(名称),甚至验证错误(规则要求至少 2 个字符,但它仍然只输入 1 个字符进行验证。如果没有字符,则仅返回 false完全添加)。
也许我的方法不是最好的,所以我愿意接受任何建议。如果您想查看困惑的源代码,您可以在此处查看正在运行的函数:http://event.gusthlm.se/make.php
最佳答案
我看到的两个问题是,您没有在输入字段上使用 name
属性,并且使用 name
作为 ID 之一(导致所有表单提交时 IE 有点悲伤)。
我建议将 name 属性添加到您的 input、select 和 textarea 元素中,并将“name”更改为“thename”之类的内容:
<label for="thename">Börjar</label>
<input type="text" name="thename" id="thename"/>
关于jquery - .valid() 仅使用 jQuery 验证插件验证第一个输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3407369/