gpt4 book ai didi

javascript - 通过(左/中/在新选项卡或窗口中打开)单击 html 链接提交表单的可能性

转载 作者:行者123 更新时间:2023-11-28 07:33:17 25 4
gpt4 key购买 nike

使用 jQuery,我想在单击 html 链接时提交 POST 表单,进入打开链接的目标浏览器选项卡

这是

<a id="submit_button" href=".">Search for values</a>
<script>
$('#submit_button').click(function() {
$('#my_form').submit();
return false;
});
</script>

如果用鼠标单击链接,则根据需要提交表单。但是

  • 如果单击中间按钮在新选项卡中打开链接,我希望看到在该新选项卡中提交的表单
  • 如果单击右键并从菜单中选择“在新选项卡中打开链接”或“在新窗口中打开链接”,我希望看到以所选方式提交的表单

有什么想法或指示如何实现这一目标吗?

最佳答案

$('#submit_button').click(function(e) {        
$(this).attr("href", "javascript:void(0)"); //reset by every click.
$('#my_form').removeAttr("target"); //reset
if (e.button == 1) //middle mouse button
{
e.preventDefault(); //stop the default behavior of the middle mouse button
$('#my_form').attr("target", "new_window");
}
$('#my_form').submit();




return false;
});

您可以尝试使用鼠标中键使用此代码,对于在新选项卡中打开的变体,您可以执行此操作。真的很脏,脏了就变好了!

$('#submit_button').contextmenu(function(e) {
window.onbeforeunload = function() {
//You do want to check here if the unload came from the right source

localStorage.setItem(name, $('#your_inputs').val());
//save your form data to local storage

}
$(this).attr("href", location.href);
//set the link to reload itself
$('#my_form').attr("target", "new_window");
//set the target to a new window
});

//put this by default on your page, when it detect local storage submit.
window.onload = function() {

var name = localStorage.getItem(name);
if (name !== null)
{
//repopulate the form and send to server using normal post
//delete the local storage items after this.
}
}

关于javascript - 通过(左/中/在新选项卡或窗口中打开)单击 html 链接提交表单的可能性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28880895/

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