簡體   English   中英

當我單擊多個列表框時,必須生成動態文本框Javascript

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM