Javascript jQuery onclick 传递帖子变量并重新加载页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2304320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery onclick pass post variables and reload page
提问by user277891
Can I pass post variables and reload a page on clicking an hyperlink?
我可以传递帖子变量并在单击超链接时重新加载页面吗?
To be clear I have something like this.
<a href="test.php?name=test">Click</a>
说清楚我有这样的事情。
<a href="test.php?name=test">Click</a>
If javascript is enabled,
如果启用了 javascript,
I think I can use "event.preventDefault()" to suppress passing as GET variable.
我想我可以使用“event.preventDefault()”来抑制作为 GET 变量的传递。
So now onclick, name should be passed as post variable instead of get.
所以现在 onclick,name 应该作为 post 变量而不是 get 传递。
If javascript is disabled, Then the above should work.
如果 javascript 被禁用,那么上面的应该可以工作。
回答by bobince
You could do it, by creating a new formelement, pointing it at the hrefand calling .submit()on it.
您可以通过创建一个新form元素,将其指向href并调用.submit()它来做到这一点。
<a class="postlink" href="test.php?name=test">Click</a>
<script type="text/javascript">
$('.postlink').click(function() {
var form= document.createElement('form');
form.method= 'post';
form.action= this.protocol+'//'+this.hostname+this.pathname;
$.each(this.search.slice(1).split(/[&;]/g), function() {
var ix= this.indexOf('=');
if (ix===-1) return;
var input= document.createElement('input');
input.type= 'hidden';
input.name= decodeURIComponent(this.slice(0, ix));
input.value= decodeURIComponent(this.slice(ix+1));
form.appendChild(input);
});
document.body.appendChild(form);
form.submit();
return false;
});
</script>
Or you could just do an AJAX request instead and reload()the page afterwards if you prefer.
或者reload(),如果您愿意,您可以只执行 AJAX 请求,然后再执行页面。
However, I'm not sure why you'd want to do this. What use is a link that's usually POSTed, except when it's not? (Not just when JS is disabled/unavailable or when it's a search engine, but also when the user middle-clicks the link or tries to right-click-bookmark it or whatever.)
但是,我不确定您为什么要这样做。通常发布的链接有什么用,除非它不是?(不仅仅是当 JS 被禁用/不可用或当它是一个搜索引擎时,而且当用户单击链接或尝试右键单击它的书签或其他任何时候。)
If all you want is something that behaves like a button to submit a POST form, better to actually use a real form and submit button, and then use CSS to restyle it to look like a link if that's what you want it to look like.
如果您想要的只是像提交 POST 表单的按钮一样的东西,最好实际使用真实的表单和提交按钮,然后使用 CSS 将其重新设置为看起来像一个链接,如果这就是您想要的样子。
回答by Christian Gnoth
Very good hint....
很好的提示....
I was first trying to send the form data via an Ajax Post call and reloading the page afterwards, but it was not working properly:
我首先尝试通过 Ajax Post 调用发送表单数据,然后重新加载页面,但它无法正常工作:
var biq_select_val = jQuery('#biq_search_select').val();
jQuery.post(window.location.href,
{ biq_amazon_item_list_search: biq_select_val},
function() {window.location.reload();}
);
Now I am using just a:
现在我只使用一个:
jQuery('#biq_amazon_item_list_search_form').submit();
and it is working fine.
它工作正常。
回答by user277891
I have some 10 links on a page. When user clicks on those links ajax-reload must take place.
我在一个页面上有大约 10 个链接。当用户点击这些链接时,必须进行 ajax-reload。
To be clear I have something like this.
说清楚我有这样的事情。
<a href="test.php?name=one">one</a>
<a href="test.php?name=Two">Two</a>
If javascript is enabled,
如果启用了 javascript,
Onclick, ajax load must take place.
Onclick,必须进行ajax加载。
If javascript is disabled, Then the above should work.
如果 javascript 被禁用,那么上面的应该可以工作。
Basically I am using nameto limit some values of my search page.
基本上我是name用来限制我的搜索页面的一些值。

