jQuery验证插件,$(“表单”)。提交不会验证表单
我有一张表格:jQuery验证插件,$(“表单”)。提交不会验证表单,jquery,jquery-validate,Jquery,Jquery Validate,我有一张表格: <form id="sForm"> <input type=text id="msg"> <input type="button" id="cli"> </form> 当我使用submit时 $(“#sForm”).submit() 表单上显示了错误消息,但仍提交了该消息。 当我单击用户功能时,如: $(“#cli”)。单击() 成功了! 发生了什么事?这是你的问题。您不能在验证方法中使用id。因此,将HTML更改
<form id="sForm">
<input type=text id="msg">
<input type="button" id="cli">
</form>
当我使用submit时
$(“#sForm”).submit()
表单上显示了错误消息,但仍提交了该消息。
当我单击用户功能时,如:
$(“#cli”)。单击()
成功了!
发生了什么事?这是你的问题。您不能在验证方法中使用
id
。因此,将HTML更改为使用name属性
检查此项这是您的问题。您不能在验证方法中使用
id
。因此,将HTML更改为使用name属性
检查此项如果您有几个问题,其中任何一个都会破坏jQuery验证插件 1) 您需要在选项周围添加一组大括号,
{}
:
$("#sForm").validate({
// your options
});
2) 您的消息
选项拼写错误为消息
3) 您的消息
选项后缺少逗号
4) 您的输入元素必须包含唯一的名称
属性:
<input type="text" name="msg" id="msg">
工作代码:
$(document).ready(function () { // <- ensure the DOM is ready
$("#sForm").validate({ // <- the braces were missing
rules: {
msg: { // <- "msg" is supposed to be the name attribute
required: true
}
},
messages: { // <- this was misspelled as "message"
msg: {
required: "please input msg"
}
}, // <- this comma was missing
errorPlacement: function (error, element) {
error.insertAfter(element);
}
});
});
<form id="sForm">
<input type="text" name="msg" id="msg" />
<input type="submit" id="cli" />
</form>
$(document).ready(function(){/您有几个问题,其中任何一个都会破坏jQuery验证插件
1) 您需要在选项周围添加一组大括号,{}
:
$("#sForm").validate({
// your options
});
2) 您的消息
选项拼写错误为消息
3) 您的消息
选项后缺少逗号
4) 您的输入元素必须包含唯一的名称
属性:
<input type="text" name="msg" id="msg">
工作代码:
$(document).ready(function () { // <- ensure the DOM is ready
$("#sForm").validate({ // <- the braces were missing
rules: {
msg: { // <- "msg" is supposed to be the name attribute
required: true
}
},
messages: { // <- this was misspelled as "message"
msg: {
required: "please input msg"
}
}, // <- this comma was missing
errorPlacement: function (error, element) {
error.insertAfter(element);
}
});
});
<form id="sForm">
<input type="text" name="msg" id="msg" />
<input type="submit" id="cli" />
</form>
$(document).ready(function(){//您编写的代码完全破坏了jQuery验证插件。请在$(“#cli”)中显示一个JSFIDLE演示。单击()
可以成功。您编写的代码完全破坏了jQuery验证插件。请在$(“#cli”)中显示一个JSFIDLE演示。单击()
可以成功。+1太好了。我正忙着制作小提琴。不过你的答案解释得很好。+1太好了。我正忙着制作小提琴。不过你的答案解释得很好。