我正在使用https://jqueryvalidation.org/plugin和https://craftpip.github.io/jquery-confirm plugin。
我有这个脚本的代码:
$("#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 () {
}
}
}
});
}
);
和表格:
<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>
页面本身index.php:
if ( isset($_POST['action']) && $_POST['action'] == "edit" ) {
echo "EDITED";
exit();
}
var_dump($_POST);
问题是该按钮提交未传递!我不知道为什么。我的var_dump说所有3个输入类型="文本",但不是按钮。
我尝试删除jQuery-confirm,没关系,所有输入和按钮类型都提交了:
$("#form1").validate({
submitHandler: function(form) {
form.submit();
}
);
我不知道如何解决。我不知道如何使用$ _post和php。
那是因为submit()
不包括提交按钮。您必须手动按下按钮的值。
关于如何完成的示例:
$('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/
ps:缓存jQuery选择器So
是有意义的 variables.push({name: $('#form1 button').attr('name'), value: $('#form1 button').attr('value')})
可能会变成
let button = $('#form1 button');
variables.push({name: button.attr('name'), value: button.attr('value')})