在文本字段中显示下拉列表的选定值 (javascript) [英] Display the selected value of a Drop down list in a text field (javascript)

查看:25
本文介绍了在文本字段中显示下拉列表的选定值 (javascript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

For Example: These are the items in a drop down list.

<select name="cmbitems" id="cmbitems">
    <option value="price1">blue</option>
    <option value="price2">green</option>
    <option value="price3">red</option>
</select>

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

When the user selects blue, i want to display the value of price1 in a text field below:

<input type="text" name="txtprice" id="txtprice" onClick="checkPrice()">

感谢您的回答

推荐答案

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

All you need to do is set the value of the input to the value of the select, in a select.onchange event handler.

var select = document.getElementById('cmbitems');
var input = document.getElementById('txtprice');
select.onchange = function() {
    input.value = select.value;
}

这里是 jsFiddle 演示的链接

Here is a link to a jsFiddle demo

这篇关于在文本字段中显示下拉列表的选定值 (javascript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆