jQuery 验证插件:输入与文本区域

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18060131/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:28:54  来源:igfitidea点击:

jQuery Validate plugin: input vs. textarea

jqueryhtmljquery-validate

提问by tim peterson

I'm wondering why there is a difference in jQuery Validate plugin's default handling of <input>vs. <textarea>. In the demo below, notice that the requiredclass makes the <input>required but not the <textarea>.

我想知道为什么 jQuery Validate 插件的默认处理<input><textarea>. 在下面的演示中,请注意required该类生成了<input>required 而不是<textarea>.

<form>
    <input class='required' /> 
    <textarea class='required' ></textarea>
    <button value='submit'>submit</button>
</form>

$('form').validate();

http://jsfiddle.net/trpeters1/BrCzA/

http://jsfiddle.net/trpeters1/BrCzA/

回答by Sparky

It has absolutely nothing to do with inputvs.textarea.

它与inputvs.完全无关textarea

See: http://jsfiddle.net/ZAaPu/2/

见:http: //jsfiddle.net/ZAaPu/2/

It's failing because each inputmusthave a unique nameattribute in order for this plugin to work properly. See the "Markup Recommendations" section of the "General Guidelines". Otherwise, only the very first element is validated and all others are ignored.

它失败了,因为每个插件都input必须有一个唯一的name属性才能使这个插件正常工作。 请参阅“通用指南”的“标记建议”部分。否则,仅验证第一个元素,忽略所有其他元素。

<form>
    <input name="thename" class='required' />
    <textarea name="another" class='required'></textarea>
    <button value='submit'>submit</button>
</form>

Working DEMO:http://jsfiddle.net/ZAaPu/

工作演示:http : //jsfiddle.net/ZAaPu/

回答by Deepak Biswal

I think it should work with required attribute. Check this demo. Correct me if I am wrong!

我认为它应该与 required 属性一起使用。检查这个演示。如果我错了,请纠正我!

回答by Yu Tem

add name attribut to textarea

将名称属性添加到 textarea

<form>
    <input class='required' /> 
    <textarea name='bla-bla' class='required'></textarea>
    <button value='submit'>submit</button>
</form>

$('form').validate();