Jquery 验证,将错误信息放在现有元素中,而不是生成 <label> 标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17029500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery validation, put error message in an existing element instead of generating a <label> tag
提问by Ray C Lin
I use Jquery validation plug-into validate these two input elements below.
When an input is invalid the plug-in will generate a label tag right after the input field, such as: <label for="company" style="">Required !</label>
下面我使用jquery验证插件来验证这两个输入元素。当输入无效时,插件会在输入字段后立即生成一个标签标签,例如:<label for="company" style="">Required !</label>
Question:
How do I put the error message in the existed <span class="help-inline">
instead of generating a new <label>
?
问题:如何将错误消息放入已存在的<span class="help-inline">
而不是生成新的<label>
?
Many thanks for your help in advance.
非常感谢您的帮助。
<script type="text/javascript">
$('#signUpForm').validate({
debug: false,
rules: {company:"required",username:"required"},
messages: {company:"Required !",username:"Required !"}
})// End of validate()
</script>
<div class="control-group">
<label class="control-label" for="company">Company Name</label>
<div class="controls">
<input type="text" name="company">
<label for="company">Required !</label> // -> this line is generated by Plug-In.
<span class="help-inline">(required)</span>
</div>
</div>
<div class="control-group">
<label class="control-label" for="username">User Name</label>
<div class="controls">
<input type="text" name="username">
<span class="help-inline">(required)</span>
</div>
</div>
回答by Sparky
It would be easier to leverage the built in options and let the plugin create your span
elements. The end result of the generated HTML will be what you're requesting.
利用内置选项并让插件创建您的span
元素会更容易。生成的 HTML 的最终结果将是您所请求的。
Use
errorElement
to change the<label>
into a<span>
.Use
errorClass
to change the default error class intohelp-inline
用于
errorElement
将 更改<label>
为<span>
.用于
errorClass
将默认错误类更改为help-inline
jQuery:
jQuery:
$(document).ready(function () {
$('#signUpForm').validate({ // initialize the plugin
errorElement: 'span',
errorClass: 'help-inline',
rules: {
company: "required",
username: "required"
},
messages: {
company: "Required !",
username: "Required !"
}
});
});
HTML:
HTML:
<form id="signUpForm">
<div class="control-group">
<label class="control-label" for="inputCompanyName">Company Name</label>
<div class="controls">
<input name="company" type="text" id="inputCompanyName" placeholder="" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputFirst">User Name</label>
<div class="controls">
<input name="username" type="text" id="username" placeholder="" />
</div>
</div>
<input type="submit" />
</form>
DEMO: http://jsfiddle.net/eRZFp/
演示:http: //jsfiddle.net/eRZFp/
BTW: Your User Name
field was missing the name
attribute, name="username"
. For this plugin to work properly, all input elements must contain a unique name
attribute.
顺便说一句:您的User Name
字段缺少name
属性name="username"
. 为了让这个插件正常工作,所有输入元素都必须包含一个唯一的name
属性。