我使用的是https://jqueryvalidation.org/插件和https://craftpip.github.io/jquery-confirm插件。
我有下面的脚本代码:
$("#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个输入都是“type=”,但不是按钮。
我试着删除jquery-confirm,它是ok的,所有的输入和按钮类型submit都会被传递:
$("#form1").validate({
submitHandler: function(form) {
form.submit();
}
);我不知道该怎么解决。我不知道如何使用$_POST和PHP来发布jsfiddle的示例。
发布于 2017-12-13 22:52:40
这是因为submit()不包含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选择器是有意义的
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')})https://stackoverflow.com/questions/47795656
复制相似问题