javascript Ajax 获取元素的 id,其中元素有多个链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13010495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 17:37:23  来源:igfitidea点击:

Ajax get id of element where there are multiple links with elements

javascriptajax

提问by Patrick

The following code is what I am currently using to try and give the posts ID to vote.php, however this currently returns [object Object]. How would I be able to pass the correct id when a link is clicked?

以下代码是我目前用来尝试将帖子 ID 提供给 vote.php 的代码,但是目前返回 [object Object]。单击链接时,我如何能够传递正确的 ID?

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
        $.ajax({  
        type: "POST",  
        data: "id=" + $(this).attr("href", "id"), 
        url: "vote.php"
        });  
</script>

<a href="javascript:;" id="1"><div id="button">Like!</div></a>
<a href="javascript:;" id="2"><div id="button">Like!</div></a>
<a href="javascript:;" id="3"><div id="button">Like!</div></a>

Thanks!

谢谢!

采纳答案by Asad Saeeduddin

You need to bind the ajax call to a click handler:

您需要将 ajax 调用绑定到点击处理程序:

$(document).on("click","a",function(e){
    $.ajax({  
    type: "POST",  
    data: "id=" + $(this).attr("id"), 
    url: "vote.php"
    });
});

回答by techfoobar

You should consider changing it to:

您应该考虑将其更改为:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
    function vote(_obj) {    
      $.ajax({  
        type: "POST",  
        data: "id=" + $(_obj).attr("href", "id"), 
        url: "vote.php"
        });  
    }
    $(document).ready(function() {
        $('.vote').click(function() {
            vote(this);
            return false;
        });
    });
</script>

<a id="1" class="vote"><div>Like!</div></a>
<a id="2" class="vote"><div>Like!</div></a>
<a id="3" class="vote"><div>Like!</div></a>

Some notes:

一些注意事项:

  1. Do not use multiple elements with the same ID on a page
  2. Bind events only after your DOM is loaded and ready, i.e. on or after the domready event.
  1. 不要在一个页面上使用多个具有相同 ID 的元素
  2. 仅在您的 DOM 加载并准备好之后绑定事件,即在 domready 事件中或之后。

回答by Salketer

In your code, you are assigning the href of the links to "id"which does not make a lot of sense... And you should use event handlers for clicks...

在您的代码中,您分配的链接的 href"id"没有多大意义......并且您应该使用事件处理程序进行点击......

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
        $(document).ready(function(){
            $('a.likeBTN').click($.ajax({  
                type: "POST",  
                data: "id=" + $(this).attr("id"), 
                url: "vote.php"
        }))});  
</script>

<a href="javascript:;" class="likeBTN" id="1"><div id="button">Like!</div></a>
<a href="javascript:;" class="likeBTN" id="2"><div id="button">Like!</div></a>
<a href="javascript:;" class="likeBTN" id="3"><div id="button">Like!</div></a>