简体   繁体   English

jQuery表单验证与动态生成的表单

[英]jQuery form validation with dynamically generated forms

I have a page with several forms which are dynamically generated using PHP. 我有一个页面,其中包含几种使用PHP动态生成的表单。 I am validating them using the jQuery Validation plugin. 我正在使用jQuery Validation插件对其进行验证。 The forms are all the same, but relate to different items so I have given all of the forms the same class so they can be validated by one function (each form also has a unique ID). 表单都是相同的,但是涉及不同的项目,因此我为所有表单提供了相同的类,以便可以通过一个函数来验证它们(每个表单也具有唯一的ID)。 But I'm having some problems: 但是我有一些问题:

  1. I would like the error messages to appear by the correct form items, but if I'm just using the form's class, the validator won't know which form the item is from. 我希望错误消息由正确的表单项显示,但是如果我仅使用表单的类,验证器将不知道该项来自哪个表单。
  2. I have a hyperlink to submit the form (and a regular submit button in <noscript> tags), and would usually use jQuery to submit the form, but again, how will jQuery know which submit link I've clicked, and which form to submit? 我有一个超链接来提交表单(以及<noscript>标记中的常规submit按钮),并且通常会使用jQuery提交表单,但同样,jQuery将如何知道我单击了哪个提交链接以及提交?

The easiest thing I can think of is to pass the form ID to the validate some how. 我能想到的最简单的方法是传递表单ID来验证方式。 Is that possible? 那可能吗?

The forms look like this: 表单如下所示:

<?php while($row= pg_fetch_row($groups)) {  ?>
<p class="error" id="error-<?php echo $row[0] ?>"></p>
<form action="../scripts/php/groups-process.php" method="post" id="editgroup-<?php echo $row[0] ?>" class="editgroup">
    <label for ="edit-<?php echo $row[0] ?>" >Edit group name:</label>
    <input type="text" class="text" size="20" maxlength="30" name="edit" id="edit-<?php echo $row[0] ?>" value="<?php echo $row[1] ?>" />
    <noscript><input type="submit" name="editgroup" value="Submit" /></noscript>
    <div id="submitcontainer-<?php echo $row[0] ?>"></div>
</form>
<?php } ?>

I would normally validate the form like this: 我通常会验证这样的形式:

$(document).ready(function(){
    $("#editgroup").validate({
        rules: {edit: {required: true, maxlength: 30}},
        messages: {edit: {required: 'Please enter a group name', maxlength: 'Please enter a shorter group name'},
        errorContainer: "p#error",
    });
    $("#submitcontainer").html('<a class="button" href="javascript:void();" id="submitlink" name="submit">Submit</a>');
    $("#submitlink").click(function() {
        $("#editgroup").submit();
    });
});

give the same class to all the form than try this, 给所有形式相同的类而不是尝试这个,

    <form id="1" class="common" method="post" action="page.php">
    <input type="text" class="common_input_class" size="20" maxlength="30" name="edit" id="whatever" value="whatever" />
    <input type="submit" name="submit" class="submit_this_form" value="submit" />
    </form>

    <form id="2" class="common" method="post" action="page.php">
         <input type="text" class="common_input_class" size="20" maxlength="30" name="edit" id="whatever" value="another value" />
          <input type="submit" name="submit" class="submit_this_form" value="submit" />
    </form>

    <script type="text/javascript">
         $(".common").submit(function(){
       var form_id = $(this).attr('id');
       var input_val = $(this).children('.common_input_class').val();
       if (input_val == '')
       {
        alert("input field is required");
        return false;
       }
});
   </script>

I ended up iterating through my result twice, so I create a form validator for each form dynamically, and then dynamically create the forms. 我最终遍历了两次结果,因此我为每个表单动态创建了一个表单验证器,然后动态创建了表单。 This was the best way I could think of to give me the control I wanted, although obviously it's slower and produces more code - not an ideal solution but it will do for this situation. 这是我想给我想要的控制权的最佳方式,尽管显然它速度较慢,并且可以生成更多代码-不是理想的解决方案,但可以在这种情况下使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM