是否可以使用锚点使表单提交 POST 样式?
<form>
<input name="foo" />
<input name="bar" />
<a href="#">Submit this form</a>
</form>
是否可以使用超链接作为POST
方法传递值?我想在单击图像时将值传递到下一页。
是否可以使用锚点使表单提交 POST 样式?
<form>
<input name="foo" />
<input name="bar" />
<a href="#">Submit this form</a>
</form>
是否可以使用超链接作为POST
方法传递值?我想在单击图像时将值传递到下一页。
Im going to leave my original answer below, why an anchor can not perform a post call. However, the question has been rephrazed, this answer will answer that question:
You can not use a anchor to submit, but its somewhat common practice to make it trigger a submit:
$('a#myAnchor').click(function(){ $('#myForm').submit() })
/// Or an image, doesn't really matter for jQuery:
$('img#myImg').click(function(){ $('#myForm').submit() })
// Or native js:
document.getElementById('myImg').onclick = function(){ document.getElemetById('myForm').submit() });
if the element you want to trigger a submit with is inside of a form:
$('element').click(function(){ $(this).closest('form').submit() });
No. Anchors are GETbecause anchors send you to an url, which makes it GET, never POST.
POST is data being send in post-format, it gets encoding in a way the server can handle it (important for images and such)
In this other topic this reply explains difference between GET and POST
If you must, and I mean REALLY must, you could use jQuery to fake a post, something allong these lines:
// THIS IS AN UNTESTED DRAFT! Just to shown you a direction:
$('a').click(function(e){
e.preventDefault();
var scriptname = this.href.replace(/\??(.*)?/, ''); // remove everything after '?'
var query = this.href.replace(/(.*)\?/, ''); // remove everything before '?'
$.post(scriptname, query);
});
But again, DONT DO THIS, unless you really REALLY have to. This will send a ajaxcall to the server, so the page wont actually change. Because this is bad practise, im not gonna show you how to make the page change on completion, you'll have to figure this out with the jQuery POST documentation