我是Javascript的新手,并试图找到解决我的问题的方法,但失败了。我的问题是,我有一个输入文本框和一个搜索提交按钮,当用户点击搜索提交按钮时,我想将用户重定向到网址http://testsearch/results.aspx?k=<value of text box k>
,例如,如果用户将“StackOverflow”放入文本框然后单击搜索按钮,我想将用户重定向到以下页面,任何解决方案?
http://testsearch/results.aspx?k=StackOverflow
<input type="text" id="k" name="k" />
<input type="submit" id="Go" value="Search" />
提前谢谢,
乔治
答案 0 :(得分:5)
您不需要javascript,只需将表单方法从POST更改为GET:
<form method=GET action="http://testsearch/results.aspx">
将神奇地附加“?k = StackOverflow”。这就是GET的形式。
答案 1 :(得分:4)
这不需要javascript,只是一个简单的html表单。
<form method="get" action="http://testsearch/results.aspx">
<input type="text" id="k" name="k" />
<input type="submit" id="Go" value="Search" />
</form>
这样就可以了。
答案 2 :(得分:1)
使用
<强> window.location 强>
转到新位置。
要获取文本框值,您可以使用
document.getElementById ( "k" ).value;
,整个代码将是
var textValue = document.getElementById ( "k" ).value;
window.location = 'http://testsearch/results.aspx?k=' + textValue;
答案 3 :(得分:1)
<input type="button" id="Go" value="Search" onclick="location.href = 'http://testsearch/results.aspx?k='+document.getElementById('k').value;" />
请注意,它是button
,而不是submit