2013-06-21 15 views
0

我有一個選擇半徑的搜索表單,我使用在線api在郵政編碼上進行搜索。 api自動提交輸入,如果輸入,但提交後,我改變了半徑,我不得不再次輸入郵政編碼,否則不會發送新的數據。是否有可能刷新選擇框的變化的html輸入?

我想知道是否可以刷新選定半徑的郵政編碼onchange的輸入。

我searchform:

<form name="input" method="post" action="searchresults" class="pro6pp_range"> 
<input type="search" onchange="validate()" placeholder="Zoeken..." name="search" size="85"> 
<input type="search" name="searchpc" required="required" class="postcode" value='<?= $postcode ?>' placeholder="Postcode (1234)" maxlength="4"> 
    <select id="selection" class="range" onchange="updateInput(this.value)"> 
     <option value="5">5 km</option> 
     <option value="10">10 km</option> 
     <option value="15">15 km</option> 
     <option value="20">20 km</option> 
     <option value="50">50 km</option> 
     </select> 
    <input type="submit" value="Zoeken"> 
    <br/> 
    <span class="message"></span>  

    <span class="output"></span> 
    <br/> 
</form> 

我想這一點,但沒有奏效:

function updateInput(searchpc){ 
     document.getElementByName("searchpc").value = <?php echo $postcode ?>; 
    } 

還應注意選擇框我的的onChange功能。

+0

你可以顯示使用API​​的代碼嗎?該API是否使用JavaScript來提交表單?頁面是否刷新? – jcubic

+0

我認爲它應該是document.getElementsByName,你不覺得它應該有索引? – Markipe

回答

0

據我所知getElementsByName將返回一個array

所以儘量在適當位置使用idname

<input type="search" name="searchpc" id="searchpc" .... /> 

而你的函數將像,

function updateInput(searchpc){ 
    document.getElementById("searchpc").value = searchpc; 
    // I think it should be selected value from select box notphp value 
} 
+0

好吧,它是令人耳目一新的,但我現在從輸入的半徑中獲得值。所以當我選擇10公里時,輸入更新爲10. –

+0

和你現在想要什麼? –

+0

我在輸入中輸入'9101'。當我選擇例如10km的值更改爲10,但我只是想刷新輸入的值,所以'9101' –

1

可能會工作

function updateInput(searchpc){ 
    document.getElementByName("searchpc").value = searchpc; 
} 

searchpc是你的「 this.value「選擇標籤。

請讓我知道它是否工作。

UPDATE

你輸入9101,然後如果你選擇10公里那麼你可以張貼的形式類似

function updateInput(searchpc) 
{ 
    document.input.submit(); 
} 

因此我們將同時獲得9101和10公里的SearchResult所頁面。

新的更新

我覺得你的小盒子輸入表單應該像以下。

<form name="small_input" action="searchresults" method="post"> 
<input type="text" placeholder="Postcode" size="25" name="postcode"/> 
<select name="radius" class="range" onchange="updateInput(this.value)"> 
    <option selected="" disabled="">Afstand</option> 
    <option value="5">5</option> 
    <option value="10">10</option> 
    <option value="15">15</option> 
    <option value="20">20</option> 
    <option value="25">25</option> 
</select> 
<br><br> 
<hr> 
<br> 
    <p style="float: right"><input type="submit" value="Opslaan"></p> 
    <input type="hidden" value="true" name="submitted"> 
    <input type="hidden" value="true" name="afstand"> 
</form> 

請讓我知道是什麼狀態。

+0

我認爲你應該使用'document.getElementsByName(「searchpc」)[0] .value = searchpc;'。因爲'getElementsByName'將返回一個'數組'。 –

+0

對不起,我想做document.getElementById(「searchpc」)。value = searchpc;抱歉,是我的錯... – ABorty

+0

你能幫我用我從Rohan的答案中使用的代碼嗎?他不活躍。 –

相關問題