简体   繁体   English

JQuery从多个锚点击以进行ajax调用

[英]JQuery click from multiple anchor for ajax call

I have multiple anchor tag generated by php from database like this 我有这样的数据库从php生成的多个锚标签

<a href="#" id="reply_doc" data-doc_value="1"></a>
<a href="#" id="reply_doc" data-doc_value="2"></a>
<a href="#" id="reply_doc" data-doc_value="3"></a>

Now I got JQuery script 现在我得到了JQuery脚本

$(document).ready(function(){
  $("#reply_doc").click(function () {
    var a = $(this).data("doc_value");
    if (a != "") {
        $.ajax({
            type: "POST",
            url: "ajax_check/",
            data: "doc_reply=" + a,
            success: function (b) {
                alert(a+' ok. '+b)
            }
        });
    }
 });
});

This is only applying for the first anchor tag. 这仅适用于第一个锚标记。 How can I do this for each anchor tag when that particular is clicked only that anchor will be affected and that particular value will be send with ajax. 如果仅点击该特定锚点将受影响且该特定值将与ajax一起发送,我如何为每个锚标记执行此操作。

Any help will be appreciated. 任何帮助将不胜感激。 thanks in advance. 提前致谢。

The id attribute is an identifier . id属性是标识符 As the name might suggest, that means it has to be unique so that it can identify a single element. 正如名称所暗示的那样,这意味着它必须是唯一的,以便它可以识别单个元素。 In order to recognise multiple elements, you'll need to use a class instead: 为了识别多个元素,您需要使用类来代替:

<a href="#" class="reply_doc" data-doc_value="1"></a>
<a href="#" class="reply_doc" data-doc_value="2"></a>
<a href="#" class="reply_doc" data-doc_value="3"></a>

$(".reply_doc").click(...);

Change the id to a class , and change your selector to ".reply_doc" . id更改为class ,并将选择器更改为".reply_doc"

Your html is currently invalid because the id attribute should be unique. 您的html目前无效,因为id属性应该是唯一的。 Although the browser doesn't jump up and down complaining when you break this rule, when you try to select an element by id it only finds the first (or, in some browsers, the last). 虽然当你破坏这个规则时浏览器不会上下抱怨,当你尝试按id选择一个元素时,它只能找到第一个(或者,在某些浏览器中,最后一个)。

If, hypothetically, you have no control over the html structure you could instead select all anchor elements with the data-doc_value attribute: 假设您无法控制html结构,则可以使用data-doc_value属性选择所有锚元素:

$("a[data-doc_value]")...

Elements cannot have same id so bind event on same class . 元素不能具有相同的id,因此在同一个类上绑定事件。

a href="#" class="option" id="reply_doc" data-doc_value="1"></a>
<a href="#" class="option" id="reply_doc" data-doc_value="2"></a>
<a href="#" id="reply_doc" class="option" data-doc_value="3"></a>

JQuery script JQuery脚本

$(document).ready(function(){
  $(".option").click(function () {
    var a = $(this).data("doc_value");
    if (a != "") {
    $.ajax({
        type: "POST",
        url: "ajax_check/",
        data: "doc_reply=" + a,
        success: function (b) {
            alert(a+' ok. '+b)
        }
    });
}
});
});

Edited again DEMO 再次编辑 DEMO

<a href="#" class="reply_doc" data-doc_value="1">111</a><br>
<a href="#" class="reply_doc" data-doc_value="2">222</a><br>
<a href="#" class="reply_doc" data-doc_value="3">333</a><br>

$(document).ready(function(){
  $(".reply_doc").click(function () {
    var a = $("a.reply_doc").attr("data-doc_value"); //<-- Add this
    if (a != "") {
    $.ajax({
        type: "POST",
        url: "ajax_check/",
        data: "doc_reply=" + a,
        success: function (b) {
            alert(a+' ok. '+b)
        }
    });
  }
 });
});

This way each time " a " will have unique value contained in data-doc_value on click 这样,每次“ a ”在click上都会包含data-doc_value中的唯一值

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

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