首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jQuery验证插件。是否验证非表单域?

问jQuery验证插件。是否验证非表单域?
EN

Stack Overflow用户
提问于 2011-11-06 23:15:46
回答 1查看 1.5K关注 0票数 0

Ні,所有人!

我有一个关于表单插件的小问题:-我可以使用jQuery.Validation插件完成对非表单字段的输入字段(即“jQuery.Validation”标签中的否)的验证吗?

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-11-06 23:41:13

可以,但是字段仍然需要在一组<form>标记中。但是,您不需要“提交”此form,以便检查其中字段的有效性。您可以使用.valid()方法独立于form提交来检查此表单。

http://jsfiddle.net/9fgVN/13/

代码语言:javascript
复制
<form id="myNonsubmitForm" action="#">
    <textarea name="comments" id="comments" rows="5" cols="30"></textarea>
</form>

<button id="myButton">Click to Check Validation Status</button>
<input type="text" id="output" />
代码语言:javascript
复制
$(document).ready(function() {

    $("#myNonsubmitForm").validate({
        validClass: 'valid',  // as per your configuration
        rules: {  // set rules as per your configuration
            comments: {
                required: false,
                maxlength: 10
            }
        },
        errorPlacement: function(error, element) {
             // use this to control placement of error messages
             // removal of errorPlacement handler will result in message appearing next to field automatically.
        }
    });

    $("#myButton").click(function() { // validate on button click for this example
        if($("#myNonsubmitForm").valid()){
            $('#output').val('passed');
        } else {
            $('#output').val('failed');
        };
    });

});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8027986

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档