Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

greasemonkey - GM_xmlhttpRequest loses data in jQuery .each() statement

I've some pages with URL like this: www.example.com/index.php?id={a number} and each page has the same <select> element:

<select id="foo">
    <option value="1">Page1
    <option value="2">Page2
    <option value="3">Page3
    <option value="4">Page4
</select>

and each page has a <ul> element with different li values:

<ul id="other">
    <li class="var1">some_value1_page[n]</li>
    <li class="var2">some_value2_page[n]</li>
</ul>

The Greasemonkey script takes the <select> element (in any page) and gets the 'id' page from <option> value, and then gets the <ul> values from every page and print in a div:

...
$('#foo option').each(function(){
    $(div).append( $(this).text() );
    var pageText = '';
    GM_xmlhttpRequest ({
        method: 'GET',
        url:    'index.php?id='+$(this).attr('value'),
        onload: function (responseDetails){
                    pageText = responseDetails.responseText;
                    $(pageText).find("#other li").each(function(){
                        $(div).append( $(this).text() );
                    });
                    $(div).append('<br />');
        },
        synchronous:    true
    });
});
...

if the script is running in index.php?id=1, prints:

Page1
Page2    some_value1_page[1]    some_value2_page[1]
Page3    some_value1_page[2]    some_value2_page[2]
Page4    some_value1_page[3]    some_value2_page[3]

if this is running in index.php?id=2, prints

Page1
Page2    some_value1_page[2]    some_value2_page[2]
Page3    some_value1_page[2]    some_value2_page[2]
Page4    some_value1_page[3]    some_value2_page[3]

if this is running in index.php?id=4, prints

Page1
Page2    some_value1_page[4]    some_value2_page[4]
Page3    some_value1_page[1]    some_value2_page[1]
Page4    some_value1_page[4]    some_value2_page[4]

some ideas??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...