javascript IE 8 不支持推送吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3277025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 00:43:51  来源:igfitidea点击:

Does IE 8 not support push?

javascriptjqueryinternet-explorercross-browser

提问by chobo2

I am trying to insert an key/value pair into a serializeArray(from jquery).

我正在尝试将键/值对插入 serializeArray(来自 jquery)。

So I have something like

所以我有类似的东西

var form = $('#form');
var sendFormData = form.serializeArray();
sendFormData.push({ "name": "Name", "value": "test"});

In firefox this works yet in IE 8 I get

在 Firefox 中,这在 IE 8 中仍然有效,我得到

Line: 51 Error: Object doesn't support this property or method

行:51 错误:对象不支持此属性或方法

So it seems to be pointing to this line. So does ie 8 not support push if so what is a way I can add a key/value pair that will work in all browsers(the 5 mains ones firefox, ie8, chrome, opera, safari)

所以它似乎指向这条线。那么 ie 8 不支持推送,如果是的话,我可以添加一个适用于所有浏览器的键/值对的方法是什么(5 个主要浏览器的 firefox、ie8、chrome、opera、safari)

回答by Nick Craver

What you have works (even in IE8), you can test it here: http://jsfiddle.net/ZAxzQ/

你有什么作品(即使在 IE8 中),你可以在这里测试:http: //jsfiddle.net/ZAxzQ/

There must be something outside the question that you're doing to get that error :)

您正在做的问题之外必须有一些事情才能得到该错误:)

.push()has been around as long as the Arrayobject, I've never seen a browser that doesn'tsupport it...your unsupported error hasto be coming from something else.

.push()只要Array对象就已经存在,我从来没有见过支持它的浏览器......你不受支持的错误必须来自其他东西。

回答by Daniel Vassallo

This is not an exhaustive answer as it won't solve your problem, but the Array.push()method works in IE8:

这不是一个详尽的答案,因为它不会解决您的问题,但该Array.push()方法适用于 IE8:

var arr = [];
arr.push({ "name": "Test Name", "value": "Test Value"});
alert(arr[0].name);    // Displays "Test Name"

The above can also be re-written as follows:

上面的也可以改写如下:

var arr = [];
arr[arr.length] = { "name": "Test Name", "value": "Test Value"};
alert(arr[0].name);    // Displays "Test Name"

回答by Psytronic

I haven't got access to IE atm, but I'm sure it does support push. Check that sendFormData is considered an array:

我无法访问 IE atm,但我确定它确实支持推送。检查 sendFormData 是否被视为数组:

Object.prototype.toString.call(sendFormData) === '[object Array]';

Something else IE likes to do, is tell you there is an error on the line after the error occurred, so it may be part of the form.serializeArray() line.

IE 喜欢做的其他事情是告诉您在错误发生后该行有错误,因此它可能是 form.serializeArray() 行的一部分。

回答by colinbashbash

I thought I had the same problem; but ended up finding that my problem was that IE7-IE8 didn't implement Array.prototype.indexOf. If you want to use that, though, you can go to this link: indexOf.

我以为我有同样的问题;但最终发现我的问题是 IE7-IE8 没有实现 Array.prototype.indexOf。但是,如果您想使用它,您可以转到此链接:indexOf

回答by Psytronic

Of course, the easiestanother solution is to do something like this:

当然,最简单的另一种解决方案是执行以下操作:

var sendFormData = $("#form").append("<input id='someuniqueID' type='hidden' name='name' value='test' />").serializeArray();
$("#someuniqueID").remove(); //optional could keep it in there if you wanted