开发者

jquery replacing spaces not working in IE

I'm sort of new to jquery so I hope somebody can help me with this problem.

I'm working on grails, and I have this jQuery object:

var tableIds = $("#myTable tbody tr td:first-child").text().toString()

when I alert() this, it has the id's some info (first column) of every row on myTable like:

1 1213 2324 23123

(to get the spaces between the id's I had to add manually the '& nbsp;' in the first td of myTable)

Now, when I try to:

var idArray = new Array()
idArray = tableIds.split(" ")

it doesn't work, the "split" just leave the idArray as a string with the original spaces

just as tableIds.

What I had to do was to replace the spaces with hyphens:

tableIds = tableIds.replace(/\s/g,"-")

and then split("-") works, dunno why... but ONLY in CHROME!! not in IE, and I need this to work in the stupid IE.

IE keeps showing me "1 1213 2324 23123", it did not found the spaces to replace and just left it like that.

Anyone have a clue on this? Hope you can help me, if not, 开发者_开发问答thanks anyway.


I think you might be going about this in the wrong way. If you want to get an array of ID's of the elements that jQuery has picked up via that selector, try doing this instead:

var ids = [];
$("#myTable tbody tr td:first-child").each(function () {
    ids.push($(this).attr('id'));
});
//now the ids array will have all of the ID's of the elements in it

The 'each' runs the passed function over every element that was selected.. so you can grab all of the ID's like that very reliably. There might be more optimized ways, but this method has never failed me.

Edit: after re-reading, I realized you're probably looking for the text id that you've given.. something like..

| 1 | dog    | $5
| 2 | cat    | $2
| 3 | fish   | $1.10

So.. using the same idea as I wrote above...

var ids = [];
$("#myTable tbody tr td:first-child").each(function () {
    ids.push($(this).text());
});
//ids will be [1, 2, 3]


Try replacing your   spaces with regular white space after each id.


Hmmm... I could be way off in assuming your mistake, but bear with me here:

It sounds like you're setting the ID of each td that you want to select to be a string with a bunch of spaces in it (or hyphens, or whatever).

jQuery's selector syntax is build so that you don't have to do this. You should be able to select the elements you want with the above syntax, and then iterate over them with an each() loop. Consider this:

var anArray = [];

$("#myTable tbody tr td:first-child").each(function(){
    //This code will run for every element matched by the above selector
    var contentsOfCell = $(this).text();
    anArray.push(contentsOfCell);
});

Hope this helps!


You can extract the contents of the first <td> of each row using jQuery's .each() to iterate over all the results from your initial selector, putting each value in the array as you go:

var idArray = [];

$("#myTable tbody tr td:first-child").each(function() {
   idArray.push($(this).text());
});

I don't know what's up with the regex not finding the spaces, unless IE thinks that non-breaking spaces are not whitespace?


Thanks to everyone! I didn't try replacing non-breaking spaces with simple spaces, becouse adding spaces manually on my TD's wasn't actually a good idea.

but as Stephen and nnnnnn and Chazbot said, this did the trick!

var ids = [];
$("#myTable tbody tr td:first-child").each(function () {
    ids.push($(this).text());
})

I didn't know you can "each" this kind of jquery objects, I should've known it :D Something new to learn.

Thanks again!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜