I am new to Javascript and try to find a solution to my issue but failed. My problem is, I have an input text box and a search submit button, and when user clicks the Search submit button, I want t开发者_开发技巧o redirect user to url http://testsearch/results.aspx?k=<value of text box k>
, for example, if user put "StackOverflow" into text box and then clicks the search button, I want to redirect user to the following page, any solutions?
http://testsearch/results.aspx?k=StackOverflow
<input type="text" id="k" name="k" />
<input type="submit" id="Go" value="Search" />
thanks in advance, George
You don't need javascript for this, just change the form method from POST to GET:
<form method=GET action="http://testsearch/results.aspx">
The "?k=StackOverflow" will magically be appended. It's what form method GET does.
That doesn't require javascript, just a simple html form.
<form method="get" action="http://testsearch/results.aspx">
<input type="text" id="k" name="k" />
<input type="submit" id="Go" value="Search" />
</form>
This will do the trick. <input type="button" id="Go" value="Search" onclick="location.href = 'http://testsearch/results.aspx?k='+document.getElementById('k').value;" />
Note that it's a button
, not a submit
Use
window.location
to go to the new location.
To get the textbox value you can use
document.getElementById ( "k" ).value;
and the whole code will be
var textValue = document.getElementById ( "k" ).value;
window.location = 'http://testsearch/results.aspx?k=' + textValue;
精彩评论