![](/img/trans.png)
[英]How do I add multiple values from a select listbox to another listbox on click of a button using javascript function?
[英]When I click multiple listbox dynamic textbox must be generated Javascript
在这里,我有多个列表框,当我单击一个选项时,应生成Javascript动态文本框。
<html>
<head>
<script type="text/javascript">
function changeFunc() {
var selectBox = document.getElementById("selectBox");
var selectedValue = selectBox.options[selectBox.selectedIndex].value;
alert(selectedValue);
}
</script>
</head>
<body>
<select id="selectBox" multiple onchange="changeFunc();">
<option value="1">Option #1</option>
<option value="2">Option #2</option>
</select>
</body>
</html>
使用JS Core的解决方案:
function changeFunc() {
var selectBox = document.getElementById("selectBox");
var input = document.createElement('input');
input.value = selectBox.options[selectBox.selectedIndex].value;
input.type = text;
document.getElementbyId('input_fields').appendChild(input);
}
将以下div添加到您的身体
<div id="input_fields"></div>
如果使用jQuery,解决方案将变得更加容易:
function changeFunc() {
$("#input_fields").append("<input type='text' value='"+$("#selectBox").val()+"'>");
}
这将只是将HTML内的id附加到id =“ input_fields”
这是你想要的吗?
<html> <head> <script type="text/javascript"> function changeFunc() { var selectBox = document.getElementById("selectBox"); var selectedValue = selectBox.options[selectBox.selectedIndex].value; var textP = document.createElement('p'); textP.innerHTML = selectedValue; document.body.appendChild(textP); } </script> </head> <body> <select id="selectBox" multiple onchange="changeFunc();"> <option value="1">Option #1</option> <option value="2">Option #2</option> </select> </body> </html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.