jQuery验证从动态表单不工作 [英] jQuery Validation From Dynamic Form Not Working

查看:127
本文介绍了jQuery验证从动态表单不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我检索与阿贾克斯的一种形式,有一次我收到的形式,并把它添加到DOM中,我尝试添加一些验证它使用jQuery,结果并不如预期。

I am retrieving a form with ajax, once i have received the form and added it to the DOM, i try to append some validation to it with jQuery, the result is not as expected.

这是我插入到网页从AJAX调用的HTML是:

The HTML that i insert into the page from an AJAX call is:

<form id="accept-form">
    <input id="txName" type="text" value="" name="txName" data-val-required="Please Select A Valid User" data-val="true">
    <span class="field-validation-valid" data-valmsg-for="txName" data-valmsg-replace="true"></span>
</form>

我再追加这种形式到我的网页,并尝试创建一个jQuery验证它,如下所示:

I then append this form to my page and attempt to create a jQuery validator for it as follows:

$("#accept-form").validate({
    rules: {
        txName: {
            required: true
        }
    },
    messages: {
        txName: {
            required: "Enter your name"
        }
    }
});

后来,当我preSS提交按钮,我有一个返回True的报警设置,无论是否txName字段为空或不:

Then when i press the submit button, i have an alert set that returns True regardless of if the txName field is empty or not:

alert($("#accept-form").valid());

尝试过几种不同的方法,将AP preciate任何想法,我可能是做错了。

Have tried several different approaches, would appreciate any ideas as to what I could be doing wrong.

推荐答案

在anwser只是我的样子分析与验证器的HTML。

the anwser is just the way i was parsing the html with the validator.

var form = $("#accept-form");

form.unbind();
form.data("validator", null);
$.validator.unobtrusive.parse(form);

这篇关于jQuery验证从动态表单不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆