jquery 怎么把下拉框选中的值 赋给文本框

如题所述

获取下拉框值可直接用$('#id').val()
例如:
<select id="names">
<option value='zhangsan'>张三</option>

<option value='lisi' selected>李四</option>
</select>
<input type="text" id="myText" value=""/>
1、获取id=names下拉框选中值
var name = $("#names").val();
2、将获取的值赋给id=myText文本框
$("#myText").val(name);
温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-03-30

下拉框和文本框的取值和赋值都是用 val() 函数,取值时参数为空,赋值时值作为参数。

    获取id=names下拉框选中值
    var name = $("#names").val();

    将获取的值赋给id=myText文本框
    $("#myText").val(name)。

本回答被网友采纳
第2个回答  2012-03-17
是很简单
$('input:text').val($('select').val());
下拉框和文本框的取值和赋值都是用 val() 函数,取值时参数为空,赋值时值作为参数
第3个回答  推荐于2017-11-25
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>

<body>
<input type="text" value="" id="get" />
<select id="orz">
<option value="1">111</option>
<option value="2">222</option>
<option value="3">333</option>
</select>
<script>
$("#orz").change(function(){
$("#get").val($(this).val());
});
</script>

</body>
</html>本回答被提问者采纳
第4个回答  2015-11-12
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>

<body>
<input type="text" value="" id="get" />
<select id="orz">
<option value="1">111</option>
<option value="2">222</option>
<option value="3">333</option>
</select>
<script>
$("#orz").change(function(){
$("#get").val($(this).val());
});
</script>

</body>
</html>