admin管理员组文章数量:1310263
The Get variables of the URL need to be parsed. I have made a jQuery object of document.location and then have used the attr function to get the search attribute to get all the variables. But when i use the split function on it and after that each() is used it gives error stating that the object does not have a method each .
TypeError: Object [object Array] has no method 'each'
Code is :
$(document.location).attr('search').split('&').each()
I have also tried to use the search property in the first function but it does not allow it i.e $(document.location.search) gives error.
I have also checked that data type of the what is returned by split function, the console says that it is an object, i am also confused that it should have been an array.
P.S : all the above code goes in document.ready function of jQuery
The Get variables of the URL need to be parsed. I have made a jQuery object of document.location and then have used the attr function to get the search attribute to get all the variables. But when i use the split function on it and after that each() is used it gives error stating that the object does not have a method each .
TypeError: Object [object Array] has no method 'each'
Code is :
$(document.location).attr('search').split('&').each()
I have also tried to use the search property in the first function but it does not allow it i.e $(document.location.search) gives error.
I have also checked that data type of the what is returned by split function, the console says that it is an object, i am also confused that it should have been an array.
P.S : all the above code goes in document.ready function of jQuery
Share Improve this question asked Apr 5, 2013 at 12:28 S. A. MalikS. A. Malik 3,6756 gold badges40 silver badges59 bronze badges 1- possible duplicate of How can I get query string values? – Chris Farmiloe Commented Apr 5, 2013 at 12:30
4 Answers
Reset to default 7Making a jQuery object from the document.location
object is pointless, because it's not a DOM element.
Just get the search
property from the object, and use the $.each
method intead of .each
as you are looping an array, not elements:
$.each(document.location.search.split('&'), function(){
...
});
Try this:
$.each($(document.location).attr('search').split('&'), function (index, value) {
alert(index + ': ' + value);
});
jQuery .each()
method is used to iterate over a jQuery object, executing a function for each matched element.
But what you get from the $(document.location).attr('search').split('&')
is a JavaScript array, which obviously has no method 'each': that is why you are getting the error.
To loop through an array in jQuery you need to use $.each()
method like above.
As far as I know, JS-arrays don't have a each()-function.
Try
var search_arr = $(document.location).attr('search').split('&');
for (var s in search_arr) {
console.log(search_arr[s];
}
instead.
jQuery's each()
function is meant to be called on its own, directly from jQuery; javascript arrays don't have it as a function, because it wasn't built that way.
I think you're looking for this:
$.each($(document.location).attr('search').split('&'), function (index, value) {
// stuff
});
本文标签: Javascript Split Function and Jquery not working togetherStack Overflow
版权声明:本文标题:Javascript Split Function and Jquery not working together - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741842931a2400611.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论