在经典Asp中选择Ajax

时间:2015-11-24 16:54:26

标签: ajax asp-classic jquery-chosen

我是Ajax和Chosen的新手所以请耐心等待。我正在尝试根据第一个选择框重新加载第二个选择框。我的所有选择框都使用了Chosen插件。

首先,这是我选择的JQuery函数。

$(function(){

   $('.TeacherName').chosen({                 max_selected_options: 1, placeholder_text_multiple: "Select A Teacher"});

 $('.Schools')    .chosen({                 max_selected_options: 1, placeholder_text_multiple: "Select A School" });

});
</script>

接下来,我从记录集中加载两个框。

<select class="Schools" name="Schools" id="schoolDrop" style="max-width:210px;" multiple="multiple"  onchange="getLocation(this.value);">

                        <% while not SchoolList.eof%>
                            <option value="<%=SchoolList("SCHOOL_NUMBER")%>"><%=SchoolList("NAME")%> </option>
                        <%SchoolList.MoveNext
                        wend%>

</select>   

<select class="TeacherName" name="TeacherName" id="TeacherName" multiple="multiple"style="max-width:150px;">
                    <option value="0"></option>

                    <%while not TeacherList.eof%>
                        <option value="<%=TeacherList("DCID")%>"</option>
                        <%TeacherList.MoveNext
                    wend%>

</select>

如您所见,我在学校选择框中的onchange事件中调用Ajax函数。

接下来是我的Ajax功能。

function getLocation(schoolId) 
{       
    var strURL="SetTeacherAjax.asp?school="+schoolId;

    var req = getXMLHTTP();

    if (req) 
    {
        req.onreadystatechange = function() 
        {
            if (req.readyState == 4) 
            {
                // only if "OK"
                if (req.status == 200) 
                {                   
                    document.getElementById('TeacherName').innerHTML=req.responseText;              
                } else 
                {
                    alert("There was a problem while using XMLHTTP:\n" + req.statusText);
                }
            }               
        }           
        req.open("GET", strURL, true);
        req.send(null);
    }       
}//End of Function getLocation

如果从TeacherName下拉列表中删除类选择器,则ajax有效。

1 个答案:

答案 0 :(得分:1)

我已经弄明白这一点,所以我会发布它,因为它可以帮助别人。我需要为Chosen插件添加更新语句。

enter code here                 if (req.status == 200) 
                {                   
                    document.getElementById('TeacherName').innerHTML=req.responseText;  
                    $('#TeacherName').trigger('chosen:updated');                                    
                } else 
                {
                    alert("There was a problem while using XMLHTTP:\n" + req.statusText);
                }

我添加了

$('#TeacherName').trigger('chosen:updated');

它有效。