2011-08-15 38 views
-3

HTML:如何通过javascript获取值?

我想将gsearch的值传递给q参数。以下是我做的方式,但它不能工作。我应该怎么做?

action="http://test.com/search.php?q=<script type="text/javascript">document.getElementById('gsearch').value;</script>"> 

更新:

在我的网站

。我想做一个谷歌自定义搜索:所以我把下面的代码放在首页。 0156290304977:8texhu0mrk谷歌搜索值。 gsearch.php网页,其中我把谷歌定制搜索代码,并显示当用户输入的gsearch文本框中搜索文本,如果他点击submi吨按钮搜索结果

<form method="get" action="http://test.com/gsearch.php?cx=0156290304977:8texhu0mrk&cof=FORID:11&ie=UTF-8&q=..." > 

    <input type="text" title="" value="" name="q" class="search-input" id="gsearch" /> 
    <input type="submit" value="" name="sa" id="search-button"/> 
    </form> 

现在,我想,。然后在gsearch.php页面上显示搜索结果。

+0

fatnjazzy答案是正确的,你要什么做是没有意义的。如果你的意思是别的,请更好地解释。 –

+0

我很抱歉,我尝试fatnjazzy answer.but它不起作用。该行动=“http://test.com//gsearch.php?cx=015290304348117:8tbxhu0mrk&cof=FORID:11&ie=UTF-8&q=theinput” – zhuanzhou

+0

是的,它的工作原理。提交后,写入文本框的值将被附加到表单操作URL。这就是HTML格式如何工作,当你有'get'方法时,这是基础。你必须错过别的东西或不解释你想要做的事情。 –

回答

0

,或者您需要在javascritpt

<script> 
function SubmitForm(){ 
window.open("http://test.com/search.php?q="+document.getElementById('gsearch').value) 
return false; 
} 
</script> 

<form method="get" action="http://test.com/search.php" onSubmit="SubmitForm();false" > 

<input type="text" title="" value="" name="q" class="search-input" id="gsearch" /> 
<input type="submit" value="" name="sa" id="search-button"/> 
</form> 
1

,如果你想提交此:http://test.com/search.php?q=theinput 只是这样做:

<form target="_top" method="get" action="http://www.cnn.com/search.php" > 

<input type="text" title="" value="theinput" name="q" class="search-input" id="gsearch" /> 
<input type="submit" value="submit" id="search-button"/> 
</form> 

<form>元素背后的整个想法是,它是确保所有的来自用户的输入将被发送到那个行动。
表格将从q中接收输入,并将其自动添加到action

所以在你的简单情况下。不需要操作。

测试在这里 http://jsfiddle.net/L4rHG/1/

这将提交给http://edition.cnn.com/search.php?q=theinput

+0

我很抱歉,我试过你的答案,但它不起作用。 'action =“http://test.com/gsearch.php?cx=0156290304977:8texhu0mrk&cof=FORID:11&ie=UTF-8&q=theinput' – zhuanzhou

+0

@zhuanzhou - 我编辑我的帖子 – fatnjazzy

+0

对不起,结果是不是我想要的,我已经更新了这个问题,谢谢 – zhuanzhou

-2
<form action="http://test.com/search.php?q="> 
<script> 
document.forms[0].action += 'new_action.html'; 
</script>