0

我在jqueryui.com/tabs/#中的选项卡式索引中的同一页面上有两个表单

我已经包含了js文件:

jquery.validate.js,附加方法.js,jquery.js

第一个 TAB 中的表格:

<form action="#" method="post" accept-charset="utf-8" name="org" id="org" autocomplete="off">         
  <table border="0" cellpadding="4" cellspacing="0">
    <tr>
       <td> <label>Name: </label></td>
       <td><input type="text" name="head" value="" id="head" class="required"  /></td>
    </tr>
    <tr>
      <td><label></label></td>
      <td><input type="submit" name="Submit" value="Add" onclick="javascript:addForm();" /></td>
    </tr>
  </table>
</form>

在第二个 TAB 中形成:

 <form action="" method="post" accept-charset="utf-8" name="orgEdit" id="orgEdit" autocomplete="off">
   <table border="0" cellpadding="4" cellspacing="0">
     <tr>
       <td><label>Head: </label></td>
       <td><input type="text" name="editHead" value="" id="editHead" class="required"  /></td>
     </tr>
     <tr>
        <td><label></label></td>
        <td><input type="submit" name="Submit" value="Update" onclick="javascript:editForm();" /></td>
     </tr>
   </table>
 </form>

单击这两个表单的提交按钮的验证部分如下,

<script>
function addForm() {
    $("#org").validate({
        rules: {
            head: {
                required:true,
                letterswithbasicpunc:true
            },
        },
        messages: { 
            head: {
                required:"&nbsp;Please enter head name.",
                letterswithbasicpunc:"&nbsp;Please enter only characters."
            },
        },
        submitHandler: function(form) {
            $.post("url",$("#org").serialize(),function(data){ 
                if(data != 'E') {
                    alert("Data Inserted Successfully.");
                    location.reload();
                } else
                    alert("Failed.");
            });
        }
    });
}

function editForm() {
    $("#orgEdit").validate({ 
        rules: {
            editHead: {
                required:true,
                letterswithbasicpunc:true
            },
        },
        messages: { 
            editHead: {
                required:"&nbsp;Please enter head name.",
                letterswithbasicpunc:"&nbsp;Please enter only characters."
            },
        },
        submitHandler: function(form) { 
            $.post("url",$("#orgEdit").serialize(),function(data){
                if(data != 'E') {
                    alert("Data Inserted Successfully.");
                    location.reload();
                } else
                    alert("Failed.");
            });
        }
    });
}
    </script>

添加数据时 jquery 验证工作正常,但在编辑表单中失败。请帮助我。

4

1 回答 1

1

您应该如下更改代码:

<form onsubmit="return editForm(this);" action="" method="post" accept-charset="utf-8" name="orgEdit" id="orgEdit"      autocomplete="off">
   <table border="0" cellpadding="4" cellspacing="0">
     <tr>
       <td><label>Head: </label></td>
       <td><input type="text" name="editHead" value="" id="editHead" class="required" /></td>
    </tr>
    <tr>
        <td><label></label></td>
        <td><input type="submit" name="Submit" value="Update" /></td>
    </tr>
   </table>
</form>

Javascript:

function editForm(form) {
    $(form).validate({ 
      rules: {
        editHead: {
            required:true,
            letterswithbasicpunc:true
        },
      },
      messages: { 
        editHead: {
            required:"&nbsp;Please enter head name.",
            letterswithbasicpunc:"&nbsp;Please enter only characters."
         },
       },
      submitHandler: function(form) { 
        $.post("url",$("#orgEdit").serialize(),function(data){
            if(data != 'E') {
                alert("Data Inserted Successfully.");
                location.reload();
            } else
                alert("Failed.");
        });
      }
    });
   return $(form).valid();
   }
于 2013-07-17T03:52:22.740 回答