doutanghuan9595 2012-05-07 00:48
浏览 36

使用多个提交按钮进行jquery验证时,无法识别第二个提交操作

I am using the jquery validate plugin to validate and submit a form on my page which has multiple submit buttons to run different functions.

One submit button runs using the $post method while the other uses the standard action method.

(Please stick with me if my terminology is wrong)

The problem I am having is that if I submit the form using the first button, then try again using the second button, it trys to run the first action again on the second submit.

Here's my code which will hopefully make things clearer...

<form id="myForm" action="add.php">
<input type="submit" id="myfunction" />
<input type="submit"​ id="add" />
​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​<input type="text" name="myvalue" />
</form>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

and my validate code...

$("#myForm input[type=submit]").click(function(e) {
        if (e.target.id == 'myfunction') {
            $("#myForm").validate({
                submitHandler: function(form) {
                    $.post('myfunctionpage.php', $("#myForm").serialize(), function(data) { });
                }
            });
        } else if (e.target.id == 'add') {
            $("#myForm").validate({
                rules: {
                    name: {
                        required: true,
                    }
                }
            });
        }

    });​
  • 写回答

3条回答 默认 最新

  • duanjiao6711 2012-05-07 00:53
    关注

    Why don't you seaprate the code into two segments?

    $("#myfunction").click(function(e) {
    
                $("#myForm").validate({
                    submitHandler: function(form) {
                        $.post('myfunctionpage.php', $("#myForm").serialize(), function(data) { });
                    }
                });
            } 
    
    $("#add").click(function(e) {
                $("#myForm").validate({
                    rules: {
                        name: {
                            required: true
                        }
                    }
                });
            }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题