在文本字段中显示下拉列表的选定值(javascript)

NLi*_*mbu 4 javascript forms drop-down-menu

例如:这些是下拉列表中的项目.

<select name="cmbitems" id="cmbitems">
    <option value="price1">blue</option>
    <option value="price2">green</option>
    <option value="price3">red</option>
</select>
Run Code Online (Sandbox Code Playgroud)

当用户选择蓝色时,我想在下面的文本字段中显示price1的值:

<input type="text" name="txtprice" id="txtprice" onClick="checkPrice()">
Run Code Online (Sandbox Code Playgroud)

谢谢你的回答

jum*_*ett 5

您需要做的就是在select.onchange事件处理程序中将输入的值设置为select的值.

var select = document.getElementById('cmbitems');
var input = document.getElementById('txtprice');
select.onchange = function() {
    input.value = select.value;
}
Run Code Online (Sandbox Code Playgroud)

是jsFiddle演示的链接