如何将项目添加到文本框中提供的下拉列表中

How to add items to a dropdown list provided from textbox?

本文关键字:下拉列表 文本 项目 添加      更新时间:2023-09-26

我想从文本框中获取值

并将它们添加到另一页的下拉列表中。

假设您有以下两种形式:

HTML:

<form id="form1">
  <input type="text" value="testing">
  <button id="addoption">Click on me</button>
</form>
<form id="form2">
  <select name="myoptions" id="myselect">
    <option value="1">first option</option>
  </select>
</form>

Jquery:

$('#addoption').click(function(e){
   e.preventDefault(); //Prevent to send the form
   var myvalue = $('input').val();
   //Now let's append an option to the select
   $('#myselect').append($("<option></option>").text(myvalue));
});

工作示例:http://jsfiddle.net/WZyQ3/1/