问题
I am trying to do a form submit from javascript. The form gets submitted but the parameters are not getting passed. Below is my code.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Tic Tac Toe</title>
<script type="text/javascript">
function selection(selected){
theform = window.document.tictactoe;
theform.action="GameAction?selection="+selected.id;
theform.submit();
}
</script>
</head>
<body>
<form name="tictactoe" id="tictactoe" method="GET" action="GameAction">
<table border="1">
<tr><td><img id="0,0" src="images/white.JPG" onclick="selection(this);"></img></td><td><img id="0,1" src="images/white.JPG" onclick="selection(this);"></img></td><td><img id="0,2" src="images/white.JPG" onclick="selection(this);"></img></td></tr>
<tr><td><img id="1,0" src="images/white.JPG" onclick="selection(this);"></img></td><td><img id="1,1" src="images/white.JPG" onclick="selection(this);"></img></td><td><img id="1,2" src="images/white.JPG" onclick="selection(this);"></img></td></tr>
<tr><td><img id="2,0" src="images/white.JPG" onclick="selection(this);"></img></td><td><img id="2,1" src="images/white.JPG" onclick="selection(this);"></img></td><td><img id="2,2" src="images/white.JPG" onclick="selection(this);"></img></td></tr>
</table>
</form>
</body>
</html>
It would be great if someone could help in resolving this issue.
回答1:
When the form gets submited through 'get', the browser takes the values of the form, makes a query string out of it and then sends the window to that location. If there was already a query string there, it doesn't just append the new values.. it wipes them out. If you change the method to a 'post', you'll notice that it does in fact add that data you wanted to have added to the URL. Since I'm not sure what exactly your end result really is, I at least hope this information helps you out. It's just based off experience, so others might have more technical insights.
来源:https://stackoverflow.com/questions/7376205/url-parameters-not-getting-passed-during-javascript-submit