jquery表单验证不适用于动态添加的表单输入

时间:2011-02-28 16:59:04

标签: javascript jquery html validation

我正在尝试验证表单,其中一部分包含动态添加的输入字段,但动态添加的字段由于某种原因未进行验证,这是我用于将字段附加到表单的代码,

使用Javascript:

//instantiate form validation
$("#form").validationEngine('attach');

$("#add_friend").live('click', function(){
    var count = $(".friend").size();
    if(count == 4){
        alert("only 4 friends allowed");
    }else{
        //append a friend field to the friend container
        $("[data-custom='refer_container']").append("<div class='friend'>"
            +"<label for='friend_"+count+"'>friend: </label>"
            +"<input type='text' data-custom='name' class='validate[required] text-input' id='friend_name_"+count+"' name='friend_name_"+count+"' />"
            +"<input type='text' data-custom='email' class='validate[required] text-input' id='friend_email_"+count+"' name='friend_email_"+count+"' />"
            +"<div class='delete'></div>"
        +"</div><br />");
    }
    $("#form").validationEngine('attach');
});

我正在使用this插件来提前验证表格,而不是!

1 个答案:

答案 0 :(得分:1)

看起来你必须先分开。否则,您的新输入不会包含在评估中。可以检查他们的API是否有动态添加(这比破坏和重建验证对象更好)

<html>
  <head>
    <link rel="stylesheet" href="http://www.position-relative.net/creation/formValidator/css/validationEngine.jquery.css" type="text/css"/>
  </head>
  <body>
    <form id="form">
        <div data-custom='refer_container'></div>
        <input id="add_friend" type="button" value="Add Friend" />
        <input type='text' data-custom='name' class='validate[required] text-input' id="test" name="test" />
    </form>
  </body>
</html>     
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" ></script>
<script type="text/javascript" src="http://www.position-relative.net/creation/formValidator/js/jquery.validationEngine-en.js" ></script>
<script type="text/javascript" src="http://www.position-relative.net/creation/formValidator/js/jquery.validationEngine.js" ></script>
<script type="text/javascript">
    //instantiate form validation 
    $(function(){
        $("#form").validationEngine('attach');  
        $("#add_friend").live('click', function(){   
            //Must Detach First
            $("#form").validationEngine('detach');  
            var count = $(".friend").size();     
            if(count == 4){         
                alert("only 4 friends allowed");     
            }else{         
                //append a friend field to the friend container         
                $("[data-custom='refer_container']").append("<div class='friend'>"             
                +"<label for='friend_"+count+"'>friend: </label>"             
                +"<input type='text' data-custom='name' class='validate[required] text-input' id='friend_name_"+count+"' name='friend_name_"+count+"' />"             
                +"<input type='text' data-custom='email' class='validate[required] text-input' id='friend_email_"+count+"' name='friend_email_"+count+"' />"             
                +"<div class='delete'></div>"         
                +"</div><br />");     
            }     
            $("#form").validationEngine('attach'); 
        }); 
    });
</script>