Jquery问题.click .each
您好,您能帮我解决这个基于 JQUERY 的代码吗?
<script>
$(function() {
$("#chatIM").each(function() {
$(this).click(function() {
$.post("chatController.php", { action:"getChat",
user:"<?php echo $loggedInUser->display_username; ?>",
other: $(this).attr("title"),
type: "<?php echo USERTYPE; ?>"},
function(data) {
$("#chatUsers").empty();
$(document.createElement("div")).attr({id: 'chatbox'}).html(data).prependTo("body");
});
});
});
});
</script>
<a id="chatIM" title="gowthami">CHAT ME</a>
<a id="chatIM" title="vaisakhi">CHAT ME</a>
我不确定问题出在哪里,但是当我按下第一个按钮时,它可以工作,但第二个按钮就不起作用(会有很多具有相同 ID 的 A 标签,因此代码必须适用于每个按钮。 也许 .each .click 或其他地方的语法错误。你能帮我吗?
Hi can you help me with this JQUERY based code?
<script>
$(function() {
$("#chatIM").each(function() {
$(this).click(function() {
$.post("chatController.php", { action:"getChat",
user:"<?php echo $loggedInUser->display_username; ?>",
other: $(this).attr("title"),
type: "<?php echo USERTYPE; ?>"},
function(data) {
$("#chatUsers").empty();
$(document.createElement("div")).attr({id: 'chatbox'}).html(data).prependTo("body");
});
});
});
});
</script>
<a id="chatIM" title="gowthami">CHAT ME</a>
<a id="chatIM" title="vaisakhi">CHAT ME</a>
I am not sure where is the problem but when I press first of those buttons it works but for the second it doesn't work (there will be a lot of A tags with the same ID so the code must work to each one.
Perhaps its a wrong syntax at .each .click or somewhere else. Can you help me whith that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果将相同的 id 分配给多个元素会导致问题,id 在文档中应该是唯一的。你可以使用类来代替 -
并且你的 jQuery 将更改为 -
It will cause problems if you assign the same id to multiple elements, ids should be unique in a document. You could use classes instead -
and your jQuery would change to -
一页上不应有多个具有相同 ID 的元素。将 id 更改为类:
You shouldn't have more than one element on a page with the same ID. Change the id to a class:
id 字段在整个文档中应该是唯一的。尝试添加如下所示的类属性:
并将 jQuery 代码更改为:
The id field should be unique across the document. Try adding a class attribute like this:
And change the jQuery code to this: