问题
I was developing on my localhost and came across this question.
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">
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
}
My question is: is something like this possible? To send a HTTP request with link press and button press simultaniously?
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.
Thanks in advance!
回答1:
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.
An <a>
would not really work, because it generates a new HTTP request and would presumably skip JavaScript execution of the currently loaded page.
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
});
来源:https://stackoverflow.com/questions/39307163/link-click-and-button-click-in-one-http-request