【问题标题】:submitting a form after jquery validate - need to pass clicked button to post data在 jquery 验证后提交表单 - 需要通过单击按钮来发布数据
【发布时间】:2020-05-20 19:06:09
【问题描述】:

我让 jquery validate 1.19.1 在表单上正常工作。 表单有多个按钮。我的目标是让单击的按钮传递给发布数据,以便我可以在表单回发中处理它。只需使用提交按钮就可以了,我可以在我的 php 中处理,例如单击插入按钮时:

    if (isset($_POST["insert"])) {
        print_r($_POST); 
        // do insert
    }

...但是对于 JQuery,这不起作用,因为它丢失了插入按钮。 我在想也许我可以在提交之前将按钮附加到表单但没有成功,请指教?我的jquery代码如下,谢谢。

jQuery(document).ready(function($){
    $('#myForm').validate({
        submitHandler: function (form) {               
            // append button here?
        }
    });
    $('insert').click(function () {
        $('[name="manufacturerSelect"], [name="chassisNameInsert"]').each(function () {
            $(this).rules('add', {
                required: true
            });
        });  
        $('#myForm').submit();  // validate and submit
    });
    $('update').click(function () {
        $('[name="manufacturerSelect"], [name="chassisNameInsert"]').each(function () {                
            $(this).rules('remove');
        }); 
        $('#myForm').submit();  // validate and submit
    });
});

【问题讨论】:

    标签: php jquery validation


    【解决方案1】:

    除非有人有更优雅的解决方案,否则我认为没关系。我让它像这样工作:

        $('#insertButton').click(function () {
            $('[name="manufacturerSelect"], [name="chassisNameInsert"]').each(function () {
                $(this).rules('add', {
                    required: true
                });
            });
            if ($('#myForm').valid()){
                var input = $('<input>')
                   .attr('type', 'hidden')
                   .attr('name', 'insert').val('true');
                $('#myForm').append($(input));   
                $('#myForm').submit();
            };            
        });
    

    【讨论】:

      猜你喜欢
      • 2019-06-06
      • 2019-07-20
      • 1970-01-01
      • 2020-06-27
      • 1970-01-01
      • 1970-01-01
      • 2012-07-13
      • 1970-01-01
      • 2016-01-21
      相关资源
      最近更新 更多