日期:2014-05-16  浏览次数:20474 次

jquery 向下拉单添加选项问题 appendTo (新手问题,添加不上了)
JScript code

function AddOption(BuMen_Id, BuMen_Name) {

    var BuMen_ID = "";
    var BuMen_NAME = ""; 

    $("#BenXiaoZuoZhe_BuMen_0").empty();
    var html = "";
    for (var i = 0; i < BuMen_Id.length; i++) {

        BuMen_ID = BuMen_Id[i].substring(1, (BuMen_Id[i].length - 1));
        BuMen_NAME = BuMen_Name[i].substring(1, (BuMen_Name[i].length - 1));
        html += "<option value=\"" + BuMen_ID + "\">" + BuMen_NAME + "</option>";
    }
    $(html).appendTo("#BenXiaoZuoZhe_BuMen_0");
    
    alert(html);
}


以上是js代码,参数:BuMen_Id,BuMen_Name分别为传过来作为value和text的数组,
用for循环解析后,形成一个这样的字符串:

<option value="SD001T0501">软件工程系</option><option value="SD001T0502">计科系02</option><option value="SD001T0503">计科系03</option>

最后一句alert出的语句。

前台:
HTML code


 <td style="text-align: center">
             <select name="BenXiaoZuoZhe_BuMen_0" runat="server">
             </select>
  </td>




我试了很多次,
方法:document.form1.BenXiaoZuoZhe_BuMen_0.options.add(new Option(BuMen_ID, BuMen_NAME));没用,行不通。前台name改为id不行,前台用
HTML code
<asp:DropDownList ID="BenXiaoZuoZhe_BuMen_0" runat="server">
</asp:DropDownList>
也不行,大家帮我看看是哪个地方出了问题??搞了一晚上了。。。

------解决方案--------------------
$(html).appendTo("#BenXiaoZuoZhe_BuMen_0");
前台需要的id没有,应该设置id,不是name
------解决方案--------------------
HTML code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>jquery 向下拉单添加选项问题</title>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
</head>
<body>
    <table>
        <tr> 
            <td style="text-align: center">
                 <select id="BenXiaoZuoZhe_BuMen_0"></select>
              </td>
          </tr>
      </table>
      <script language="javascript">
        function AddOption(BuMen_Id, BuMen_Name) 
        {
            var BuMen_ID = "";
            var BuMen_NAME = ""; 
            $("#BenXiaoZuoZhe_BuMen_0").empty();
            var html = "";
            for (var i = 0; i < BuMen_Id.length; i++) {
        
                BuMen_ID = BuMen_Id[i].substring(0, (BuMen_Id[i].length));
                BuMen_NAME = BuMen_Name[i].substring(0, (BuMen_Name[i].length));
                html += "<option value=\"" + BuMen_ID + "\">" + BuMen_NAME + "</option>";
            }
            $("#BenXiaoZuoZhe_BuMen_0").append(html);    
            alert(html);
        }
        var BuMen_Id = new Array('SD001T0501','SD001T0502','SD001T0503');
        var BuMen_Name = new Array('软件工程系','计科系02','计科系03');
        window.onload=AddOption(BuMen_Id,BuMen_Name);
    </script>
</body>
</html>

------解决方案--------------