dongmei8209 2015-09-18 06:50
浏览 51
已采纳

如何使用javascript填充下拉列表?

My javascript:

    var countrygroup = [
    "AFG-Afghanistan",
    "ALA-Åland Islands",
    "ALB-Albania",
    "DZA-Algeria",
    "ASM-American Samoa",
    "AND-Andorra",
    "AGO-Angola",
    "AIA-Anguilla",
    "ATA-Antarctica",
    "ATG-Antigua and Barbuda",
    "ARG-Argentina",
    "ARM-Armenia",
    "ABW-Aruba",
    "AUS-Australia"
    ];

    $(function() {

    $.each( countrygroup, function( i, a ){
        var sep = a.split('-');
        $("#country").append('<option value="' + sep[0] + '">' + sep[1] +'</option>');
    });
    });

php:

    <form action="validation.php" method="post">
        <table width="250" align="center">
        <tr>
            <td>UserId:</td>
            <td><input type="text" name="userId" required></td>
        </tr>
        <tr>
            <td>Country Code:</td>
            <td><span class="input">
                 <select name="country" id="country" style="width:100%; white-space:nowrap">
                      <option value="NIL">Country</option>
        <!-- Use jquery to populate options -->
                 </select>
                </span>
             </td>
        </tr>
        <tr>
              <td>Password:</td>
              <td><input type="password" name="pwd" required></td>
       </tr>
      <tr align="left">
             <td>&nbsp;</td>
             <td>&nbsp;&nbsp;<input type="submit" value="Login"></td>
      </tr>
    </table>
</form>
 <script src="js/dropdown_script.js" type="text/javascript"></script>

I want to populate the country list into my dropdown list name country. However all it show was the word "country" in the dropdownlist. What's wrong with my codes? The other time i use the same codes seem to be working but not this. Did i miss out something?

  • 写回答

2条回答 默认 最新

  • doulu8446 2015-09-18 06:58
    关注

    You need to load the jQuery library. Preferably at the bottom (because of efficiency loading reasons), just before loading your javascript (which should NOT be in the head as someone suggested).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来