如何根据JavaScript中的条件控制窗体的动作属性?
我有一个PHP表单。现在我已经在它的action属性中定义了硬编码的值。现在的场景是,我正在表单提交上调用一个JavaScript函数。在该函数中,我使用了如何根据JavaScript中的条件控制窗体的动作属性?,javascript,jquery,forms,submit,Javascript,Jquery,Forms,Submit,我有一个PHP表单。现在我已经在它的action属性中定义了硬编码的值。现在的场景是,我正在表单提交上调用一个JavaScript函数。在该函数中,我使用了confirm()函数 根据confirm的值,即true或false,我想将表格提交到下一页。简而言之,我想根据confirm()的输出值将表单提交到两个PHP页面中的任意一个 如果你愿意,我可以给你我的代码 那么,您可以使用jQuery覆盖提交函数: var sendURL = "send/to/url.php" $('#myForm'
confirm()
函数
根据confirm的值,即true或false,我想将表格提交到下一页。简而言之,我想根据confirm()
的输出值将表单提交到两个PHP页面中的任意一个
如果你愿意,我可以给你我的代码 那么,您可以使用jQuery覆盖提交函数:
var sendURL = "send/to/url.php"
$('#myForm').submit(function() {
// get all the inputs into an array.
var $inputs = $('#myForm :input');
var sendData = "";
$inputs.each(function() {
sendData += this.name + "=" + $(this).val();
});
//confimation
var conf = confirm("hello world");
if(conf){
sendURL="send/to/url/1.php";
} else {
sendURL="send/to/url/2.php";
}
$.ajax({
url: sendURL,
type: 'post',
data: sendData,
success: function (data) {
//do stuff when ajax succeeds
}
});
});
在javascript代码中将sendURL变量更改为任意值…$(“#form_id”).attr(“action”,“anywhere.php”);