JS下拉列表框

有一个下拉列表框 <select> <option>可以添加<option> </select> <input type="text" value="点我"> 按 点我 在下拉列表框当中添加一个新的option 显示的名称为输入框当中的文字

<!-- 程序如下 这里引入的js为1.7.2,你可以引入其它版本的js-->

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>下拉框添加内容</title>
<script src='js/jquery-1.7.2.min.js'></script>
</head>
<body>
    <select id='sel'>
        <option>可以添加</option>
    </select>
    <input type='text' class='inp' value='' />
    <button>点我</button>
    
    <script>
        $("button").click(function(){
            var inputvalue = $('.inp').val();
            if(inputvalue == ''){alert('请先填写好内容在点击我哦!');return false;}
            var htmlstr = "<option>"+inputvalue+"</option>";
            $("#sel").append(htmlstr);
            
        });
    </script>
</body>
</html>


<!-- 程序结束,下面是程序的运行结果 -->


温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-01-15

在input 里面加个onclick 进js方法,然后 取得select 增加<opction>  ,<option>的value为

你取得的文本框的内容.



<html>
  <head>    
    <title>a</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
   <script type="text/javascript">

function add(){
          //根据id查找对象,
           var obj=document.getElementById('mySelect');
           //添加一个选项
            alert("1");
         obj.options.add(new Option(document.getElementById('wbk').value,"2")); 
         alert("2");
   }
  </script>
  </head>
  
  <body>
  <form>
  <input type='text' name='wbk' id='wbk' value='点我' onclick="add()"/>
  <select id='mySelect' name='mySelect'>
   <option>可以添加</option> 
  </select>
  </form>
  </body>
 
</html>


参考下面的连接:

http://www.2cto.com/kf/201303/197989.html