【问题标题】:ajax internet explorer onchangeajax Internet Explorer onchange
【发布时间】:2012-08-17 16:01:52
【问题描述】:

我用于加载第二个选择框的 ajax 脚本在 firefox 和 chrome 中工作,但内部资源管理器无法处理它。我从我的选择框中调用 onChange 函数,并将选择框中的值赋给该函数。

代码:

function getXMLHTTP() 
{
var xmlhttp;

if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

return xmlhttp;
}

function getType(categoryName) 
{       

    var strURL="includes/get.php?c="+categoryName+"&sid="+Math.random();
        var req = getXMLHTTP();

        if (req) {

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

我的第二个问题是,是否可以在选项标签之间发送文本而不是选项标签中的值?

【问题讨论】:

  • 最好使用 AJAX 框架(例如 jQuery)来处理跨浏览器问题。尽量不要重新发明轮子。
  • 是的,但它只是这个小脚本而已

标签: javascript html ajax internet-explorer


【解决方案1】:

对于您的第一个问题,我假设您的 if (req) 返回 false。你用的是哪个IE版本?尝试在getXMLHTTP() 函数中添加调试代码以开始诊断代码。试试微软提供的这个解决方案:http://msdn.microsoft.com/en-us/library/ms537505(v=vs.85).aspx

我尽量不重复别人的回答。以下是在选定选项标签中获取文本的答案:Getting the text from a drop-down box

【讨论】:

  • 我尝试使用 Microsoft 的解决方案,但仍然无法正常工作。对于 firefox 和 chrome,一切都可以正常工作,但在 IE 9 或 8 中则无法正常工作。
  • 以前没用过jquery,我可以试试,但我不知道如何在jquery中实现当前代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-27
  • 1970-01-01
  • 2023-03-15
  • 2019-08-08
  • 1970-01-01
相关资源
最近更新 更多