简体   繁体   English

无法在IE中添加,在Firefox和Chrome中均可使用

[英]Cannot append in IE, works in Firefox and Chrome

I think IE hates me. 我认为IE讨厌我。 Everything I do in other browsers works, but IE 11, no! 我在其他浏览器中所做的所有工作都可以,但是IE 11不能! It is failing. 失败了。 I'm guessing it's me and not the browser. 我猜是我本人,而不是浏览器。

I have a <select> list on my web page, and I'm trying to bind the <option> dynamically. 我的网页上有一个<select>列表,并且试图动态绑定<option>

So far, it works great in Firefox and Chrome, but not in IE. 到目前为止,它在Firefox和Chrome中运行良好,但在IE中则无法运行。 There is no error in the IE Development tools (F12) so I'm lost as to why it refuses to work. IE开发工具(F12)中没有错误,因此我迷失了为什么它无法工作。

JSFIDDLE JSFIDDLE

HTML 的HTML

<select id="MyList">
</select>

Javascript Java脚本

$(function() {
    var myItems=[];
    myItems.push({"All":"Hello"});
    myItems.push({"One":"And More"});
    myItems.push({"Two":"Lots More"});   

    for (i = 0; i< myItems.length;i++)  {
        for (item in myItems[i]) {
            var x = myItems[i][item]; //for my testing
            var y = myItems[item];    //for my testing
            $("#MyList").append("<option>" + item + "</option>");
        }
    }
});

If you try the fiddle in FF or Chrome, all good. 如果您尝试使用FF或Chrome的小提琴,一切都很好。 The select list populates with All, One and Two. select列表将填充“全部”,“一”和“二”。

Short of a gift or even some form of sacrifice to the IE elves, what do I need to do to make IE happy with me? 缺少给IE精灵的礼物或什至某种形式的牺牲,我需要做些什么来使IE对我满意?

This is bizarre. 真奇怪 When I run that fiddle in the IE I have handy (IE9), I get an "access denied" error and jQuery isn't loaded. 当我在IE(IE9)中运行该提琴时,出现“访问被拒绝”错误,并且未加载jQuery。 If I change the fiddle to use 1.11.0 rather than 1.10.1, I don't get that error and the script runs. 如果我将小提琴更改为使用1.11.0而不是1.10.1,则不会收到该错误,脚本将运行。

There are a couple of issues with the code, primarily undeclared variables (your code was falling prey to The Horror of Implicit Globals ); 代码有两个问题,主要是未声明的变量(您的代码已成为“隐式全球性恐怖”的牺牲品); here's an updated fiddle with: 这是更新的小提琴

  • Variable declarations 变量声明
  • Using jQuery 1.11.0 使用jQuery 1.11.0
  • Using the "no wrap - body" option rather than "onload", since you're using the ready event 因为您使用的是ready事件,所以使用“ no wrap-body”选项而不是“ onload”

Here's the code: 这是代码:

$(function() {
    var myItems=[];
    myItems.push({"All":"Hello"});
    myItems.push({"One":"And More"});
    myItems.push({"Two":"Lots More"});

    var i, item;
    for (i = 0; i< myItems.length;i++)  {
        for (item in myItems[i]) {
            var x = myItems[i][item];
            var y = myItems[item];
            $("#MyList").append("<option>" + item + "</option>");
        }
    }
});

Separately, though, to be compatible with a wider range of browsers, you might want to use the Option constructor and the add method of the options list rather than appending a DOM element; 但是,为了与更大范围的浏览器兼容,您可能想要使用Option构造函数和options列表的add方法,而不是附加DOM元素; some older browsers prefer it. 一些较旧的浏览器更喜欢它。 That looks like this: Fiddle 看起来像这样: 小提琴

// Before the loop...
var options = $("#MyList")[0].options;

// ...in the loop
options.add(new Option(item));

JSFIDDLE JSFIDDLE

var sel = $("#MyList");
sel.empty();                
for (var i = 0; i< myItems.length;i++)  {
    for (var item in myItems[i]) {
        var x = myItems[i][item];
        var y = item;
        console.log(x);
        console.log(y);            
        sel.append('<option value="' + x + '">' + y + '</option>');
    }
}

As already mentioned by @ TJCrowder in IE 10 jquery1.10.1 is not working and updating the jquery version to later it started to work; 正如IE 10中的@ TJCrowder所提到的那样,jquery1.10.1无法正常工作,并且将jquery版本更新为以后可以开始工作;

Try this 尝试这个

$(function() {
    var myItems=[];
    myItems.push({"All":"Hello"});
    myItems.push({"One":"And More"});
    myItems.push({"Two":"Lots More"});


    for (i = 0; i< myItems.length;i++)  {
        for (item in myItems[i]) {
            var x = myItems[i][item];
            $('#MyList')
             .append($("<option></option>")
             .attr("value",x)
             .text(item)); 
        }
    }
});

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

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