简体   繁体   English

jQuery确认插件不在jQuery验证内提交按钮type =“ submit”吗?

[英]Jquery-confirm plugin doesn't submit button type=“submit” inside jquery-validation?

I'm using https://jqueryvalidation.org/ plugin and https://craftpip.github.io/jquery-confirm plugin. 我正在使用https://jqueryvalidation.org/插件和https://craftpip.github.io/jquery-confirm插件。

I've this code of script: 我有这段脚本代码:

$("#form1").validate({
  submitHandler: function(form) {

            $.confirm({
                title: 'Confirm',
                content: 'Are you sure?',
                buttons: {
                    ok: {
                        text: "OK",
                        btnClass: 'btn-success',
                        action: function () {           
                            form.submit();
                        }
                    },
                    cancel: {
                        text: "Annulla",
                        action: function () {
                            }
                        }
                }
            });     


        }
);

and the form: 以及形式:

<form id="form1" name="form1" action="index.php" method="post">
  <input type="text" name="var1" value="1">
  <input type="text" name="var2" value="2">
  <input type="text" name="var3" value="3">
  <button type="submit" class="btn btn-info" value="edit" name="action">EDIT</button>
</form>

the page itself index.php: 页面本身index.php:

if ( isset($_POST['action']) && $_POST['action'] == "edit" ) {
   echo "EDITED";
   exit();
}

var_dump($_POST);

The problem is that button submit is not passed!! 问题是按钮提交没有通过!! I don't know why. 我不知道为什么 My var_dump says there are all 3 inputs type="text" but not the button. 我的var_dump说,所有3个输入都为type =“ text”,但没有按钮。

I tried removing jquery-confirm and it's ok, all inputs and button type submit are passed: 我尝试删除jquery-confirm,没关系,所有输入和按钮类型提交均通过:

$("#form1").validate({
  submitHandler: function(form) {
            form.submit();
        }
);

I don't know how to solve. 我不知道该怎么解决。 I don't know how to post an example with jsfiddle using $_POST and PHP. 我不知道如何使用$ _POST和PHP使用jsfiddle发布示例。

Thats because submit() does not include the submit button. 那是因为submit()不包含submit按钮。 You'd have to push the value of the button manually. 您必须手动按下按钮的值。

An example on how this might be done: 有关如何完成此操作的示例:

$('document').ready(function() {
$("#form1").validate({
  submitHandler: function(form) {

            $.confirm({
                title: 'Confirm',
                content: 'Are you sure?',
                buttons: {
                    ok: {
                        text: "OK",
                        btnClass: 'btn-success',
                        action: function () { 
                            let variables = $('#form1').serializeArray();
                            variables.push({name: $('#form1 button').attr('name'), value: $('#form1 button').attr('value')})

                            // Perform custom XHR here ($.ajax) with `variables`

                        }
                    },
                    cancel: {
                        text: "Annulla",
                        action: function () {
                            }
                        }
                }
            });     


        }
});
});

https://jsfiddle.net/n2gwjvqd/2/ https://jsfiddle.net/n2gwjvqd/2/

PS: it makes sense to cache jquery selectors so PS:缓存jquery选择器很有意义,因此

variables.push({name: $('#form1 button').attr('name'), value: $('#form1 button').attr('value')})

might become 可能成为

let button = $('#form1 button');
variables.push({name: button.attr('name'), value: button.attr('value')})

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

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