javascript能否做到直接将字符串插入到select里面去啊,比如以下代码,我希望加入到select中,谢谢
function ddd(){
 var option='<option value='ddd'>aaaa</option>';
}

解决方案 »

  1.   

    不用这么写
    var y=document.createElement('option');
    y.text='Kiwi';
    y.value='hello';
    var x=document.getElementById("mySelect");
      try
        {
        x.add(y,null); // standards compliant
        }
      catch(ex)
        {
        x.add(y); // IE only
        }
      

  2.   

     $("#selectid").append("<option value='ddd'>'aaaa'</option>");
      

  3.   

    TRY:<html>
    <head>
    <script type="text/javascript">
    function insertOption()
      {
      var y=document.createElement('option');
      y.text='Kiwi'
      y.value='11';
      var x=document.getElementById("mySelect");
      try
        {
        x.add(y,null); // standards compliant
        }
      catch(ex)
        {
        x.add(y); // IE only
        }
      }
    </script>
    </head>
    <body><form>
    <select id="mySelect">
      <option>Apple</option>
      <option>Pear</option>
      <option>Banana</option>
      <option>Orange</option>
    </select>
    <input type="button" onclick="insertOption()" value="Insert option" />
    </form></body>
    </html>
      

  4.   

    var selectElement = document.getElementById('node');//先取到select node
    var option = document.createElementt('option');//创建插入节点
    option.innerHTML = 'XXX';//添加内容
    selectElement.appendChild(option);//插入到select node中
      

  5.   

    <select id="s">
    </select>
    请输入要插入的合法字串:<input type="text" id="opt" value="<option value='ddd'>aaaa</option>" />
    <input type="button" value="add" onclick="add();" />
    <script text="text/javascript">
    function add(){
        try{
    document.getElementById("s").innerHTML = document.getElementById("s").innerHTML + document.getElementById("opt").value;
         }
        catch(e){
            alert(e);
         }
    }
    </script>
      

  6.   

    $("<option value='test'>test</option>").append("");document.createElement("");
    innerText="";
    .value="";
    appendChild();
      

  7.   

    我来个另类的:
    $('<option value='ddd'>aaaa</option>').appendTo($('yourselectName'));