我想从组合框到我的搜索表单获得价值

时间:2014-12-16 05:52:38

标签: javascript html html5

我有一个像这样的组合框:

<select>
    <option value="product">Product</option>
    <option value="company">Supplier</option>
</select>

我希望从组合框到我的搜索表单获得价值

<span class="search_input_box">
    <input type="text" name="q" id="SearchKeyword" value="What are you looking for..." onblur="if (this.value=='') this.value='What are you looking for...';" onfocus="if (this.value='What are you looking for...') this.value='';" />
</span>

我尝试在组合框中使用onchange="document.getElementById('hdModule').value=this.value",但它没有用。

1 个答案:

答案 0 :(得分:1)

<select>代码

上使用此代码
onchange="document.getElementById('hdModule').value=this.value"

您正在更改hdModule的值而不是文本框(SearchKeyword)。您需要更改文本框的值。将您的代码更改为

<select onchange="document.getElementById('SearchKeyword').value=this.value">
    <option value="product">Product</option>
    <option value="company">Supplier</option>
</select>

<span class="search_input_box">
    <input type="text" name="q" id="SearchKeyword" value="What are you looking for..." onblur="if (this.value=='') this.value='What are you looking for...';" onfocus="if (this.value='What are you looking for...') this.value='';" />
</span>

演示:http://jsfiddle.net/kogmyLda/

修改

如果您不想在首次加载时选择ProductSupplier,请添加一个空选项,如下所示

<select onchange="document.getElementById('SearchKeyword').value=this.value">
    <option></option>
    <option value="product">Product</option>
    <option value="company">Supplier</option>
</select>

<span class="search_input_box">
    <input type="text" name="q" id="SearchKeyword" value="What are you looking for..." onblur="if (this.value=='') this.value='What are you looking for...';" onfocus="if (this.value='What are you looking for...') this.value='';" />
</span>

演示:http://jsfiddle.net/kogmyLda/1/