我有一个选择下拉列表,我想做一些事情,当用户选择选择下拉列表中的值时,所选文本将进入输入框(我可以将其隐藏并可以发布)。
到目前为止我有这个代码:
<select name="klant" id="selectList" style="width: 250px">>
<option value="0000">---Select Customer---</option>
<?php echo $options;?>
</select>
<input type="text" id="mytext">
我有以下 javascript 来完成这项工作,但这不起作用。输入框中没有插入任何内容。
<script type="text/javascript">// <![CDATA[
$('#selectList').val();
$('#selectList :selected').text();
var foo = $('#selectList :selected').text();
document.getElementById("mytext").value = foo ;
// ]]>
</script>
我做错了什么?
最佳答案
您错过了 onChange 事件监听器
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$( "#selectList" ).change(function() {
$('#selectList').val();
$('#selectList :selected').text();
var foo = $('#selectList :selected').text();
document.getElementById("mytext").value = foo ;
});
})
</script>
</head>
<body>
<h1>Hello, World!</h1>
<select name="klant" id="selectList" style="width: 250px">
<option value="0000">---Select Customer---</option>
<option value="0001">asadad</option>
</select>
<input type="text" id="mytext">
</body>
</html>
关于javascript,输入框中选定的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48906226/