简体   繁体   English

使用Javascript载入下拉值

[英]Using Javascript to load dropdown values

I have following code where I am trying to load values into a dropdown from an array(from file page students.js): 我有以下代码尝试在其中将值从数组(从文件页面students.js)加载到下拉列表中:

student.js student.js

var STU_NAME = 0;
var STU_ID = 1;
var STU_AGE = 2;
var STUDENTS = [
    ["Apple", 1, 15],
    ["Billy", 2, 16]
    ["Cathy", 3, 14]        
        ];

Functions.js Functions.js

var jQ = $.noConflict();

function populateStudents() {
    jQ('#students').empty();
    jQ('#students').append(jQ("<option></option>").attr("value", "").text("-- Please Select --"));
    for (var _i = 0; _i < students.length; _i++) {
        jQ('#students').append(jQ("<option></option>").attr("value", _i).text(STUDENTS[_i][STU_NAME]));
    }
}

jQ(document).ready(function () {
    populateStudents();
});

mypage.aspx mypage.aspx

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">

    </style>
</head>
<body>
    <div id="model">
        <form>
        <div>
            <div id="StudentLabel">
                Student:
            </div>
            <select id="students" name="students">
            </select>
        </div>      
        <script src="../Scripts/Functions.js" type="text/javascript"></script>
        <script src="../Scripts/student.js" type="text/javascript"></script>
        </form>
    </div>
</body>
</html>

I get an error "Microsoft JScript runtime error: '$' is undefined" at line 1 of Functions.js 我在Functions.js的第1行收到错误“ Microsoft JScript运行时错误:'$'未定义”

Can anyone help me on what I would be missing to make it working here. 任何人都可以帮助我解决在这里无法正常使用的问题。

You have forgotten to add the jquery library 您忘记添加jQuery库

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"></script>
<script src="../Scripts/Functions.js" type="text/javascript"></script>
<script src="../Scripts/student.js" type="text/javascript"></script>

It is highly recommended to use CDN version of jquery to improve pagespeed, check this link 强烈建议使用CDN版本的jquery来提高页面速度, 请检查此链接

You need to include jquery library , you can download from here Add the script tag in head of page or before you use jquery. You need to include jquery library ,您可以从此处下载在页面顶部或使用jquery之前添加script标签。

<head runat="server">
    <title></title>
    <style type="text/css">

    </style>
   <script language="javascript" type="text/javascript" src="JS/jquery-1.7.2.js"></script>
    <script src="../Scripts/Functions.js" type="text/javascript"></script>
    <script src="../Scripts/student.js" type="text/javascript"></script>
</head>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM