一个HTTP请求中的链接单击和按钮单击 [英] Link click and button click in one HTTP request
问题描述
我在本地主机上开发时遇到了这个问题.
I was developing on my localhost and came across this question.
考虑有这样的HTML代码:
Consider there is such HTML code:
<form action="" method="POST">
<input type="submit" name="submitButton" id="submitButton">
</form>
<a onclick="clickButton('submitButton');" href="guestbook/index.php?pageNumber=1">
< a>
单击触发触发器提交"按钮单击事件.Javascript函数如下所示:
The <a>
click triggers submit button click event. The Javascript function looks like this:
function clicktButton(id) {
if (id != "")
document.getElementById(id).click(); // this will trigger the click event
}
我的问题是:这样可能吗?要通过链接同时按下按钮和按钮来发送HTTP请求?
My question is: is something like this possible? To send a HTTP request with link press and button press simultaniously?
因为通过按下< a>
链接,我已经发送了带有 $ _ GET ['pageNumber']
参数的HTTP请求.但是我也想同时发送 $ _ POST ['submitButton']
数据.
Because by pressing an <a>
link I already send a HTTP request with $_GET['pageNumber']
parameter. But I also want to send the $_POST['submitButton']
data at the same time.
提前谢谢!
推荐答案
您想要的东西是可能的,但与我之前看到的代码相比,却没有太多.
What you want is possible, but not so much with the code I see before me..
<form action="guestbook/index.php?pageNumber=1" method="post">
<input type="submit" name="submitButton" id="submitButton">
<input type="button" name="submit">
</form>
这实际上将发布表单的内容,并附加获取请求.
This would essentially post the contents of the form, with a get request attached.
< a>
不会真正起作用,因为它会生成一个新的HTTP请求,并且可能会跳过当前加载页面的JavaScript执行.
An <a>
would not really work, because it generates a new HTTP request and would presumably skip JavaScript execution of the currently loaded page.
另一方面,通过向表单发送到 guestbook/index.php?pageNumber的表单数据的发布请求,您可以使用JavaScript模拟
.但是,如果您这样做,我建议您使用 jQuery ,因为通过将它添加到click事件中,它将使您更轻松./p>< a>
点击的效果= 1
On the other hand, you can simulate the effects of an <a>
click with JavaScript, by making a post request with the form data to guestbook/index.php?pageNumber=1
. But if you do this I would recommend jQuery as it will make things easier for you by adding this to the click event.
$.ajax({
url: "guestbook/index.php?pageNumber=1",
data: {'field':'value'},
async: false
});
这篇关于一个HTTP请求中的链接单击和按钮单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!