使用jQuery和AJAX适用于所有功能,除了一个,非常奇怪的问题(从我的角度来看)

时间:2010-04-29 16:32:20

标签: javascript ajax asp-classic jquery

我正在开发一个经典的asp表单,它有很多下拉列表。其中三个是级联的,即它们依赖于之前的下拉列表。对于几乎所有代码都可以正常工作,其中一个不好玩。

首先,我有一个脚本标记,其中包含以下内容:

    $(document).ready(function () {
        $("#AcademicLevel").change(getList);
        $("#CourseDeliveryTime").change(updateLocation);
        $("#ProgramType").change(updateEntryTerm);
    });

这适用于表单的前两个元素,AcademicLevel和CourseDeliveryTime,但是第三个元素却没有生效。如果我使用Firebug的控制台并运行相同的代码行,$(“#ProgramType”)。change(updateEntryTerm);它开始工作,排序。

会发生什么让我感到困惑。如果它指向的函数updateEntryTerm中有一个alert()调用,它就可以工作。如果警报被注释掉,则不起作用。功能如下:

    function updateEntryTerm() {
        $.ajax({
            type: "POST",
            url: "../Classic ASP and AJAX/jQueryExample.asp",
            dataType: "application/x-www-form-urlencoded",
            data: "Action=UpdateEntryTerm&acadLevel=" + $("#AcademicLevel").val() + "&courseTime=" + $("#CourseDeliveryTime").val() + "&programType=" + $("#ProgramType").val(),
            async: false,
            success: function (msg) {
                $("#EntryTerm").remove();
                $("#tdEntryTerm").append(msg);
                //alert(msg);
            } //,
            //error: function (xhr, option, err) {
            //    alert("XHR Status: " + xhr.statusText + ", Error - " + err);
            //}
        });
    }

我在这里遇到两个不同的问题,首先是为什么要调用$(“#ProgramType”)。change(updateEntryTerm);除非我在Firebug控制台中运行它,否则无法正常工作?其次,除非存在alert()调用,为什么函数本身updateEntryTerm不起作用?

以前有人看起来像这样吗?

编辑:完整的javascript在这里:

<script type="text/javascript" language="javascript">
//<![CDATA[

    $(document).ready(function () {
        $("#AcademicLevel").change(getList);
        $("#CourseDeliveryTime").change(updateLocation);
        $("#ProgramType").change(updateEntryTerm);            
        //alert("DOM loaded!");
        //$("#CourseDeliveryTime").change(updateEntryTerm);
    });

    // Function to get ProgramofInterest list
    function getList() {
        $.ajax({
            type: "POST",
            url: "../Classic%20ASP%20and%20AJAX/jQueryExample.asp",
            dataType: "application/x-www-form-urlencoded",
            data: "Action=GetList&Val=" + $("#AcademicLevel").val(),
            async: false,
            success: function (msg) {
                //$("label[id$=Two]").add("select[id$=Two]").remove();
                $("#ProgramofInterest").remove();
                $("#tdProgramOfInterest").append(msg);
            }
        });
    }

    function updateLocation() {
        $.ajax({
            type: "POST",
            url: "../Classic%20ASP%20and%20AJAX/jQueryExample.asp",
            dataType: "application/x-www-form-urlencoded",
            data: "Action=UpdateLocation&acadLevel=" + $("#AcademicLevel").val() + "&courseTime=" + $("#CourseDeliveryTime").val(),
            async: false,
            success: function (msg) {
                //$("label[id$=Two]").add("select[id$=Two]").remove();
                $("#ProgramType").remove();
                $("#tdProgramType").append(msg);
            } //,
            //error: function (xhr, option, err) {
            //    alert("XHR Status: " + xhr.statusText + ", Error - " + err);
            //}
        });
    }

    function updateEntryTerm() {
        $.ajax({
            type: "POST",
            url: "../Classic%20ASP%20and%20AJAX/jQueryExample.asp",
            dataType: "application/x-www-form-urlencoded",
            data: "Action=UpdateEntryTerm&acadLevel=" + $("#AcademicLevel").val() + "&courseTime=" + $("#CourseDeliveryTime").val() + "&programType=" + $("#ProgramType").val(),
            async: false,
            success: function (msg) {
                $("#EntryTerm").remove();
                $("#tdEntryTerm").append(msg);
                //alert(msg);
            } //,
            //error: function (xhr, option, err) {
            //    alert("XHR Status: " + xhr.statusText + ", Error - " + err);
            //}
        });
    }
//]]>
</script>

这是应该获取更改事件的ProgramType表单字段的代码:

    <td id="tdProgramType">
        <!--<span id="prefLoc">
            <select id="ProgramType" name="ProgramType" onchange="changeStartTerm()">
                <option value="" >&lt;select above options first&gt;</option>
            </select>
        </span>-->
        <%
            Dim outputProgramTypes                                    

            outputProgramTypes = outputProgramTypes + "<select name=""ProgramType"" id=""ProgramType""><option value=''>&lt;Select&gt;</option>"

            Set oRs=Server.CreateObject("adodb.recordset")

            'if Request.Form("AcademicLevel") = "undergraduate" and Request.Form("CourseDeliveryTime") = "Day" then
                strSQL = "SELECT LocationName, LocationCode FROM tbLocations ORDER BY LocationWeight ASC"
            'else
            '    strSQL = "SELECT LocationName, LocationCode FROM tbLocations WHERE LocationWeight < 3 ORDER BY LocationWeight ASC"
            'end if

            oRs.Open strSQL, conn

            Do while not oRs.EOF               
                outputProgramTypes = outputProgramTypes + "<option value = '" & oRS ("LocationCode") & "'>"
                outputProgramTypes = outputProgramTypes + oRs("LocationName") & "</option>"
                oRs.MoveNext
            loop

            outputProgramTypes = outputProgramTypes + "</select>"

            Response.Write(outputProgramTypes)
        %>
    </td>
</tr>

这是帖子页面中的代码,用于切换列表中的项目:

<%

' Get the variables from the query string
dim act 
act = lcase(Request("Action"))
dim value 
value = Request("Val")

dim acadLevel, courseTime, programType
acadLevel = Request("acadLevel")
courseTime = Request("courseTime")
programType = Request("programType")

Dim oRs, conn, connect, strSQL

set conn=server.CreateObject ("adodb.connection")
connect = "Driver=SQL Server;Server=(local);Database=leads;UID=x;PWD=x;"
conn.Open connect

' Route based on the action
if act = "getlist" then
    GetProgramList(value)
elseif act = "updatelocation" then
    UpdateLocation acadLevel, courseTime
elseif act = "updateentryterm" then
    UpdateEntryTerm acadLevel, courseTime, programType
end if

' Gets the list of programs for the ProgramofInterest dropdown.
sub GetProgramList(val)
    dim outputPrograms

    outputPrograms = outputPrograms + "<select style=""width:431px;"" id=""ProgramofInterest""><option value=''>&lt;Select&gt;</option>"

    Set oRs=Server.CreateObject("adodb.recordset")

    Select Case val
        Case "undergraduate"
            strSQL = "SELECT ProgramCode, ProgramName FROM tbPrograms WHERE ProgramLevel = 'UG' ORDER BY ProgramWeight ASC, ProgramType ASC, ProgramName ASC"
        Case "graduate"
            strSQL = "SELECT ProgramCode, ProgramName FROM tbPrograms WHERE ProgramLevel = 'GR' ORDER BY ProgramWeight ASC, ProgramType ASC, ProgramName ASC"
        Case Else
            strSQL = "SELECT ProgramCode, ProgramName FROM tbPrograms ORDER BY ProgramWeight ASC, ProgramType ASC, ProgramName ASC"
    End Select

    oRs.Open strSQL, conn

    Do while not oRs.EOF               
        outputPrograms = outputPrograms + "<option value = '" & oRS ("ProgramCode") & "'>"
        outputPrograms = outputPrograms + oRs("ProgramName") & "</option>"
        oRs.MoveNext
    loop    

    outputPrograms = outputPrograms + "</select>"

    response.write(outputPrograms)  
end sub

sub UpdateLocation(level, time)
    Dim outputProgramTypes                                                  

    outputProgramTypes = outputProgramTypes + "<select name=""ProgramType"" id=""ProgramType""><option value=''>&lt;Select&gt;</option>"

    Set oRs=Server.CreateObject("adodb.recordset")

    'if Request.Form("AcademicLevel") = "undergraduate" and Request.Form("CourseDeliveryTime") = "Day" then    
    if level = "undergraduate" and time = "Day" then    
        strSQL = "SELECT LocationName, LocationCode FROM tbLocations WHERE LocationWeight < 3 ORDER BY LocationWeight ASC"        
    else
        strSQL = "SELECT LocationName, LocationCode FROM tbLocations ORDER BY LocationWeight ASC"
    end if

    oRs.Open strSQL, conn

    Do while not oRs.EOF               
        outputProgramTypes = outputProgramTypes + "<option value = '" & oRS ("LocationCode") & "'>"
        outputProgramTypes = outputProgramTypes + oRs("LocationName") & "</option>"
        oRs.MoveNext
    loop

    outputProgramTypes = outputProgramTypes + "</select>"

    Response.Write(outputProgramTypes)
end sub

sub UpdateEntryTerm(level, time, progType)
    Dim outputEntryTerms    

    outputEntryTerms = outputEntryTerms + "<select name=""EntryTerm"" id=""EntryTerm""><option value=''>&lt;Select&gt;</option>"

    Set oRs=Server.CreateObject("adodb.recordset")

    'if Request.Form("AcademicLevel") = "undergraduate" and Request.Form("CourseDeliveryTime") = "Day" and Request.Form("ProgramType") = "MAN" then
    if level = "undergraduate" and time = "Day" and progType = "MAN" then
        strSQL = "SELECT EntryTermName, EntryTermCode FROM tbEntryTerms WHERE ID < 7 ORDER BY EntryTermWeight ASC"
    else
        strSQL = "SELECT EntryTermName, EntryTermCode FROM tbEntryTerms WHERE ID = 7"        
    end if

    oRs.Open strSQL, conn

    Do while not oRs.EOF               
        outputEntryTerms = outputEntryTerms + "<option value = '" & oRS ("EntryTermCode") & "'>"
        outputEntryTerms = outputEntryTerms + oRs("EntryTermName") & "</option>"
        oRs.MoveNext
    loop

    outputEntryTerms = outputEntryTerms + "</select>"

    Response.Write(outputEntryTerms)
end sub

%>

希望这可以帮助你们帮助我一点点好转。我没有添加完整的表单,因为它很庞大,并且包含大量遗留代码。

3 个答案:

答案 0 :(得分:0)

我从未见过,但问题似乎很简单;你的活动是在实际完成之前完成的。当然,我确信解决方案涉及的更多(我很抱歉,我真的不知道?)但这是我开始寻找的地方。

答案 1 :(得分:0)

我发现了两件事:

  1. 你的网址没有被转义(即空格不是%20),虽然我不确定jQuery是否会处理它。
  2. 命名似乎表明#EntryTerm#tdEntryTerm的顶级元素。如果是这样,删除它显然会破坏发布数据到子级元素,但保持警报完整。
  3. 除此之外,您还必须发布更多代码。

答案 2 :(得分:0)

我终于在这里找到了问题,最后它很简单。

在document.ready上我为一个下拉列表添加一个函数调用更改事件,如下所示:    $( “#PROGRAMTYPE”)发生变化(updateEntryTerm);

稍后当需要更新该下拉列表时,我将其删除,然后将其添加回具有更新列表的页面。那就是问题所在。删除它之后,我再也没有调用将函数调用添加到change事件中。

就像我说的那么简单,只是忽略了那里的过程。