通过jQuery的Ajax自动填充Select Field

时间:2012-06-11 17:39:21

标签: jquery asp-classic

我在经典ASP中编码,并使用ADO访问我的数据库。我有两个选择字段,即:

<select name="NBSCourse" class="NBSCourse"></select>
<select name="IndexNo" class="IndexNo"></select>

对于“NBSCourse”,选项字段通过SQL请求填充,列出所有可用课程代码的不同结果。我想根据所选的“NBSCourse”值填充下一个选择字段“IndexNo”。

目前,我尝试了以下方法:将nbsCourseid的选定字段传递给ASP文件“indexnodrop

$("select.NBSCourse").change(function () {
var nbsCourseid;
nbsCourseid = $("select.NBSCourse").val();

 $.ajax({url:"indexnodropdown.asp?q="+nbsCourseid,success:function(result) { 

    $("select.IndexNo").html(result);

}})

 })

在indexnodrop.asp中,我已经发送了将脚本执行到数据库中。

dim selectedcourseID
selectedcourseID = Request.querystring("q")

oCmd.CommandText = "SELECT DISTINCT IndexNo FROM NBSCourse WHERE CourseId='" &  selectedcourseID & "'"
Set oRS = oCmd.Execute() 

If Not oRS.EOF Then 
response.write("<option value='" & oRS("IndexNo") & "'>" & oRS("IndexNo") & "</option>")
End If

这里有什么我想念的吗?在此先感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

$("select[name=NBSCourse]").change(function () {
  var nbsCourseid;
  nbsCourseid = this.value; // or $(this).value();
   $.ajax({url:"indexnodropdown.asp?q="+nbsCourseid,success:function(result) { 
     $("select[name=IndexNo]").html(result);
  }};
});

你不能使用

select.NBSCourse因为.符号用于访问class,但您使用了name属性。

根据评论

如果您使用class而不是name,则可以使用

$("select.NBSCourse").change(function () {
  var nbsCourseid;
  nbsCourseid = this.value; // or $(this).value();
   $.ajax({url:"indexnodropdown.asp?q="+nbsCourseid,success:function(result) { 
     $("select.IndexNo").html(result);
  }};
})