简体   繁体   中英

Get value of div via jQuery

I have 2 constantly updating spans with coordinates in them. These spans are updated from a separate js file that someone else wrote that is VERY sensitive to changes.

I can see the spans just fine and they are updating the coordinates just fine, however I need to pass them as variables though in a link. For example, I have a link on page some.php?var1=[value of constantly updating div1]&var2=[value of constantly updating div2] that I need the user to be able to click.

I need this link to, when clicked, either gets the value of the spans and submits it to a page handler ( some.php in this case) or is just constantly updated from the spans. I've tried to get the innerHTML value of the spans, it doesn't seem to be working though to constantly update them....

使用.text()函数从span onClick中获取文本值-http: //api.jquery.com/text/

Let's say your <span> elements have IDs:

<span id="var1">(constantly updating)</span>
<span id="var2">(constantly updating)</span>

Then just construct a placeholder link:

<a href="#" id="link">click me!</a>

And the following jQuery should do the trick:

$(document).ready(function() {
    $('#link').click(function() {
        window.location = 'some.php?var1=' + 
            $('#var1').text() + '&var2=' + $('#var2').text();
    });
});

You also might want to consider using encodeURIComponent() on the values of your <span> elements to make sure they get passed correctly, unless you're always sure that the format of the value is URI-safe.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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