简体   繁体   English

Internet Explorer 8.0中的Ajax

[英]ajax in internet explorer 8.0

i have written code according to which on basis of first dropdown second dropdown is populated the code is working in mozilla but it is not working in internet explorer 8.0 我已经编写了代码,根据该代码,根据第一个下拉列表中的第二个下拉列表填充该代码,该代码在mozilla中有效,但在Internet Explorer 8.0中不起作用

function stateListOnChange(str1) {

var xmlhttp;
if (str1 == "") {
    document.getElementById("CityList").innerHTML = "";
    return;
}
if (str1 == "") {
    document.getElementById("CityList").disabled = true;
}
else {
    document.getElementById("CityList").disabled = false;
}
if (window.XMLHttpRequest) {
    xmlhttp = new XMLHttpRequest();
}
else {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        document.getElementById("CityList").innerHTML = xmlhttp.responseText;
     }
}
xmlhttp.open("GET", "../includes/xml_cities.asp?q1=" + str1, true);
xmlhttp.send();

but the code is not working in ie 8.0 please give me the solution 但是代码无法在IE 8.0中运行,请给我解决方案

Can you replace this: 您可以替换为:

if (window.XMLHttpRequest) {
    xmlhttp = new XMLHttpRequest();
}
else {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

with this: 有了这个:

try  
{  
    // Internet Explorer 
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)  
{  
    try   
    { 
        // Firefox, Opera 8.0+, Safari  
        xmlHttp=new XMLHttpRequest();
    }  
    catch (e)    
    {   
        try     
        {      
            xmlHttp= new ActiveXObject("MSXML2.XMLHTTP.3.0");
        }    
        catch (e)      
        {      
            alert("Your browser does not support AJAX!");      
            return false;      
        }    
    }  
}

This code will provide more options for initializing the request, and will provide feedback when an unsupported browser is used. 该代码将提供更多用于初始化请求的选项,并在使用不受支持的浏览器时提供反馈。

add the jQuery script into your code 将jQuery脚本添加到您的代码中

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>

then use 然后使用

function stateListOnChange(state) {
    if (state == "") {
        $("#CityList").html("").prop("disabled", true);
        return false;
    }
    else {
        $("#CityList").prop("disabled", false);
    }

    $.get("../includes/xml_cities.asp", { "q1" : state }, function(data) {
        $("#CityList").html(data);
    }); 
}

and you hook up the event, either 然后您挂起事件,或者

<input id="CityList" ... onchange="stateListOnChange(this.value);" />

or simply using jQuery as well 或者只是简单地使用jQuery

$(document).ready(function() {
     $("#CityList").bind("change", function() {
         stateListOnChange( $(this).val() );
     });
});

and it works in any browser ... even a mobile one ! 它可以在任何浏览器中使用甚至可以在移动 浏览器中使用

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

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