我想將我的組合框的選定值傳遞給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"
\t href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script
\t src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script
\t src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
\t <div class="container">
\t \t <h1>Form</h1>
\t \t <form method="post" id="form" name="form">
\t \t \t <div class="form-group row">
\t \t \t \t <label class="col-xs-3 control-label" style="margin-top: 10px;">Choice</label>
\t \t \t \t <div class="col-xs-5 selectContainer">
\t \t \t \t \t <select id="combobox" class="form-control">
\t \t \t \t \t \t <option value="1">1</option>
\t \t \t \t \t \t <option value="2">2</option>
\t \t \t \t \t </select>
\t \t \t \t </div>
\t \t \t \t <input id="val" type="hidden" name="val">
\t \t \t \t <input id="btn" class="btn btn-info" value="Submit">
\t \t \t </div>
\t \t </form>
\t </div>
\t <script type=text/javascript>
\t \t $(document).ready(function() {
\t \t \t $('#btn').click(function(e) {
\t \t \t \t $('#combobox').change(function() {
\t \t \t \t \t var selected = $('option:selected', $(this)).text();
\t \t \t \t \t alert($('#val').val(selected));
\t \t \t \t });
\t \t \t });
\t \t });
\t </script>
</body>
</html>
你嵌套了您的事件處理程序。點擊更改。這總是表明代碼是錯誤的。 –
歡迎使用堆棧溢出。請注意,在發佈類似答案時(不是最後一次添加的答案),可以自定義第一個答案。 –