简体   繁体   English

通过jQuery获取div的价值

[英]Get value of div via jQuery

I have 2 constantly updating spans with coordinates in them. 我有2个不断更新的跨度,其中包含坐标。 These spans are updated from a separate js file that someone else wrote that is VERY sensitive to changes. 这些跨度是从另一个人写的对更改非常敏感的单独js文件更新的。

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. 例如,我在页面some.php?var1=[value of constantly updating div1]&var2=[value of constantly updating div2]上有一个链接,我需要用户点击该链接。

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. 我需要此链接,当单击该链接时,它要么获取跨度的值并将其提交给页面处理程序(在本例中为some.php ),要么只是从跨度中不断更新。 I've tried to get the innerHTML value of the spans, it doesn't seem to be working though to constantly update them.... 我试图获取span的innerHTML值,尽管不断更新它们似乎没有用。

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

Let's say your <span> elements have IDs: 假设您的<span>元素具有ID:

<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: 而以下jQuery应该可以解决问题:

$(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. 您可能还需要考虑在<span>元素的值上使用encodeURIComponent() ,以确保正确传递它们,除非您始终确保该值的格式是URI安全的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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