将我的组合框的选定值传递给另一个输入值

时间:2016-12-24 11:02:42

标签: javascript jquery

我想将我的组合框的选定值传递给id为" val"的输入值。

我尝试使用jquery

传递值
 $(document).ready(function() {
            $('#btn').click(function(e) {
                $('#combobox').change(function() {
                    var selected = $('option:selected', $(this)).text();
                    alert($('#val').val(selected));
                });

            });
        });

但它没有用。请帮忙。



<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"
	href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script
	src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script
	src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
	<div class="container">
		<h1>Form</h1>
		<form method="post" id="form" name="form">
			<div class="form-group row">
				<label class="col-xs-3 control-label" style="margin-top: 10px;">Choice</label>
				<div class="col-xs-5 selectContainer">
					<select id="combobox" class="form-control">
						<option value="1">1</option>
						<option value="2">2</option>
					</select>
				</div>
				<input id="val" type="hidden" name="val"> 
				<input id="btn" class="btn btn-info" value="Submit">
			</div>
		</form>
	</div>

	<script type=text/javascript>
		$(document).ready(function() {
			$('#btn').click(function(e) {
				$('#combobox').change(function() {
					var selected = $('option:selected', $(this)).text();
					alert($('#val').val(selected));
				});

			});
		});
	</script>
</body>
</html>
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:0)

.change不是必需的。在按钮上单击,您可以直接访问下拉列表的选定值。请检查以下代码。

&#13;
&#13;
		$(document).ready(function() {
			$('#btn').click(function(e) {
					alert($('#combobox option:selected').val());
			});
		});
&#13;
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"
	href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script
	src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script
	src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
	<div class="container">
		<h1>Form</h1>
		<form method="post" id="form" name="form">
			<div class="form-group row">
				<label class="col-xs-3 control-label" style="margin-top: 10px;">Choice</label>
				<div class="col-xs-5 selectContainer">
					<select id="combobox" class="form-control">
						<option value="1">1</option>
						<option value="2">2</option>
					</select>
				</div>
				<input id="val" type="hidden" name="val"> 
				<input id="btn" class="btn btn-info" value="Submit">
			</div>
		</form>
	</div>
</body>
</html>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您需要删除#btn

中的点击事件
$('#combobox').change(function() {
    var selected = $('option:selected', $(this)).text();
    $('#val').val(selected)
    alert($('#val').val());
});

https://jsfiddle.net/

答案 2 :(得分:0)

您已嵌套事件处理程序。点击内更改。这始终是代码错误的标志。您将在每次点击时添加一个新的更改处理程序,这不是您想要的。

你不是100%清楚自己想做什么,但你可以这样开始:

$(function() {
   $('#combobox').change(function() {
       var selected = $(this).val();
       $('#val').val(selected);
       alert($('#val').val());
   });
});

注意:val()上的select会返回所选值,因此您无需使用option:selected及其text

答案 3 :(得分:0)

请试一试。

&#13;
&#13;
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"
	href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script
	src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script
	src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
	<div class="container">
		<h1>Form</h1>
		<form method="post" id="form" name="form">
			<div class="form-group row">
				<label class="col-xs-3 control-label" style="margin-top: 10px;">Choice</label>
				<div class="col-xs-5 selectContainer">
					<select id="combobox" class="form-control">
						<option value="1">1</option>
						<option value="2">2</option>
					</select>
				</div>
				<input id="val" type="hidden" name="val"> 
				<input id="btn" class="btn btn-info" value="Submit">
			</div>
		</form>
	</div>

	<script type=text/javascript>
		$(document).ready(function() {
			
				$('#combobox').change(function() {
					var selected = $('option:selected', $(this)).text();
$('#val').val(selected)
alert($('#val').val());
				

			});
		});
	</script>
</body>
</html>
&#13;
&#13;
&#13;