【发布时间】:2020-01-04 19:09:54
【问题描述】:
我正在尝试在表单提交上传递一个按钮值以及一个 jquery 确认对话框,但它没有通过。我知道这与使用“form.submit”函数的脚本有关,但我对 JS 不够熟悉,无法编写解决问题的方法。
有没有办法将第一个按钮的值重新分配给一个新的 JS 变量并传递它?
我已将表单简化为基本元素,以便于理解。在此示例中,我尝试传递的数据是“123”,使用与确认对话脚本绑定的按钮。添加了第二个“常规按钮”以演示成功的表单提交。一些 PHP 收集 POST 数据并显示它。
// Results:
// $regularbutton = '456';
// $alertbutton = '';
testpage.php:
<html>
<head>
<script type="text/javascript" src="//code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="//code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<script type="text/javascript">
//<![CDATA[
$(function () {
'use strict';
function confirmDialog(title, message, success) {
var confirmdialog = $('<div></div>').appendTo('body')
.html('<div><h6>' + message + '</h6></div>')
.dialog({
modal: true,
title: title,
zIndex: 10000,
autoOpen: false,
width: 'auto',
resizable: false,
buttons: {
Yes: function () {
success();
$(this).dialog("close");
},
No: function () {
$(this).dialog("close");
}
},
close: function() {
$(this).remove();
}
});
return confirmdialog.dialog("open");
}
$('#submit_alert').on('click', function (e) {
e.preventDefault();
var form = document.getElementById('form_alert');
confirmDialog('Confirm', 'Are you sure you want to proceed?', function () {
form.submit();
});
});
});
//]]>
</script>
<link rel="stylesheet" type="text/css" href="//code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css">
</head>
<body>
<?php
extract($_POST);
echo "button1: $alertbutton <br>";
echo "button2: $regularbutton <br>";
?>
<form method="post" id="form_alert" action="testpage.php">
<button name="alertbutton" value="123" id="submit_alert">Alert Button</button>
<button name="regularbutton" value="456">Regular Button</button>
</form>
</body>
</html>
【问题讨论】:
标签: javascript jquery forms variables submit