使用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.
}
}