gpt4 book ai didi

jquery - 目标ajax提交到弹出窗口

转载 作者:行者123 更新时间:2023-12-01 08:16:33 25 4
gpt4 key购买 nike

我有一个表单需要在提交之前进行验证,POST 到弹出窗口,然后需要重置表单。

我知道 target="newWindowName"onsubmit="window.open('','newWindowName','')" 表单属性有效,但是这个提交后不让我做任何事情。

我知道我可以使用 $('form').ajaxSubmit() 来指定提交后函数,但它似乎不允许我打开新窗口。

我怎样才能同时完成所有这些事情?

这是我的表格:

<form id="myForm" target="newWindow" autocomplete="on" action="/myUrl" method="post">

这是我的 JavaScript:

$('#myForm').submit(function(e) { 
e.preventDefault();
if ($('#myForm').valid()) {
var options = {
target: '',
beforeSubmit: function () {
this.target = "newWindow";
window.open("", "newWindow", "width=500,height=450");
},
success: function () {
hideForm();
$('#myForm').resetForm();
}
};

$(this).ajaxSubmit(options);
}
return false;
}

最佳答案

这是我最终采用的解决方案,它更加优雅。

<form id="myForm" target="newWindow" autocomplete="on" action="/myUrl" method="post">

然后是 JS:

$('#myForm').submit(function(e) {
if ($(this).valid()) {
var f = this;
window.open("",$(this).attr("target"),"width=500,height=500");
setTimeout(function () { // delay resetting the form until after submit
hideForm();
f.reset();
}, 0);
return true;
}
else {
e.preventDefault(); // only prevent default if the form is not valid
}
return false;
});

这样,只有表单有效时才会显示新窗口。

关于jquery - 目标ajax提交到弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10289997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com