jquery点击不保留价值?

发布于 2024-12-16 11:45:39 字数 612 浏览 7 评论 0原文

我遇到这种情况:

$id = 123456;

<a href="<?=$id?>" id="id"></a>
<a href="100" id="count">100</a>

$('#count').live("click", function() {
    var votes = $("#count").attr("href");
    var id= $("#id").attr("href");
    var dataStrings = 'id=' + id+ '&votes=' + votes;
});

如果我执行警报或控制台日志,我只能得到计数值:id=undefined&votes=100

关于问题可能是什么的任何想法吗?

谢谢

编辑:

$id var 是一个 php 变量。如果我查看源代码,我会看到那里有回显的数字:

<a href="123456" id="id"></a>
<a href="100" id="count">100</a>

i have this situation:

$id = 123456;

<a href="<?=$id?>" id="id"></a>
<a href="100" id="count">100</a>

$('#count').live("click", function() {
    var votes = $("#count").attr("href");
    var id= $("#id").attr("href");
    var dataStrings = 'id=' + id+ '&votes=' + votes;
});

if i do a alert or console log i get only the count value: id=undefined&votes=100

any ideas on what the problem might be?

thanks

edit:

the $id var is a php one. and if i view source i see the number being echoed there:

<a href="123456" id="id"></a>
<a href="100" id="count">100</a>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

舂唻埖巳落 2024-12-23 11:45:39

您的 html 中可能有另一个元素具有重复的 id="id" 但在您在此处发布的代码之前没有“href”属性。

It is possible that you have another element in your html with a duplicate id="id" but without "href" attribute before the code you posted here.

闻呓 2024-12-23 11:45:39

只是猜测,但我想说部分问题可能是您对 href 的数值的奇怪使用。我会将这些值设为其他属性。

Just a guess, but I'd say part of the problem could be your odd use of numeric values for hrefs. I'd make those values some other attribute.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文