2011-08-15 11 views
-3

HTML :자바 스크립트로 값을 가져 오는 방법은 무엇입니까?

gsearch의 값을 q 매개 변수로 전달하고 싶습니다. 다음은 내가 만드는 방법이지만 작동하지 않습니다. 어떻게해야합니까?

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

업데이트 : 내 사이트에

. 나는 Google 맞춤 검색을 만들고 싶다. 그래서 다음 코드를 홈페이지에 넣었다. 0156290304977:8texhu0mrk Google 검색 값입니다. 난에서 구글 맞춤 검색 코드를 삽입하고 사용자의 입력이 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 페이지 ,. 그러면 gsearch.php 페이지에 검색된 결과가 표시됩니다. 이 제출하려는 경우

+0

fatnjazzy 대답은 올바른 Javascript의 이상 필요 할 말이 없다. 다른 것을 의미한다면, 더 잘 설명하십시오. –

+0

미안하지만, 나는 fatnjazzy 대답을 시도.하지만 그것은 작동하지 않습니다. action = "http://test.com//gsearch.php?cx=015290304348117:8tbxhu0mrk&cof=FORID:11&ie=UTF-8&q=theinput" – zhuanzhou

+0

예. 작동합니다. 일단 제출되면 텍스트 상자에 쓰여진 값이 양식 활동 URL에 추가됩니다. 이것이 get 메소드가있을 때 HTML 폼이 작동하는 방식입니다. 근본적입니다. 당신은 다른 것을 놓치거나 당신이하려는 것을 설명하지 않아야합니다. –

답변

0

에 제출 될 것이다 또는 당신은 당신이 무슨 말을 하려는지,

<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> 
관련 문제