你有几个问题,其中任何一个都会破坏 jQuery Validate 插件......
1)您需要{ }
在您的选项周围添加一组大括号:
$("#sForm").validate({
// your options
});
2) 您的messages
选项拼写错误为message
.
3)您的messages
选项后缺少逗号。
4)您的输入元素必须包含唯一name
属性:
<input type="text" name="msg" id="msg">
5)如果您将输入更改type="button"
为 a type="submit"
,您将不必担心使用 aclick
或submit
处理函数。该插件将自动捕获submit
事件。
<input type="submit" id="cli">
工作代码:
$(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);
}
});
});
HTML 标记:
<form id="sForm">
<input type="text" name="msg" id="msg" />
<input type="submit" id="cli" />
</form>
工作演示:http: //jsfiddle.net/kPKqQ/