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条)

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示