简体   繁体   English

仅首次“点击”触发

[英]Only first 'click' firing

I have a page which allows a user to 'nudge' someone to do something 我有一个页面,允许用户“轻推”某人做某事

I have the following html (it can appear many times but I'll show two for now) 我有以下html(它可能出现很多次,但现在我将显示两个)

<div class="nudgeHolder641">
    <a id="nudge" data-wl_id="641" data-nudge_for="415" data-nudge_from="63" href="#">Nudge</a>
</div>

<div class="nudgeHolder1172">
    <a id="nudge" data-wl_id="1172" data-nudge_for="415" data-nudge_from="63" href="#">Nudge</a>
</div>

I have the following code to action the click: 我有以下代码来操作点击:

$(document).ready(function(){
    $("#nudge").click(function() {

        var nudge_from = $( '#nudge' ).data( 'nudge_from' );
        var nudge_for = $( '#nudge' ).data( 'nudge_for' );
        var wl_id = $( '#nudge' ).data( 'wl_id' );

        var dataString = 'nudge_from='+ nudge_from + '&nudge_for=' + nudge_for + '&wl_id=' + wl_id;

        $.ajax({
            type: "POST",
            url: "/pages/includes/ajax/nudge.php",
            data: dataString,
            success: function() {

                $('.nudgeHolder'+wl_id).html("<h3>Fantastic!</h3>")
                .append("<p>Nudge Sent!</p>")
                .hide()
                .fadeIn(1500, function() {
                  //$('#message').append("<img id='checkmark' src='/images/icons/check.png' />");
                });
            }
        });
    return false;
    });
});

Only the first instance of the link fires when clicked though, when I click the second 'nudge' link nothing happens, the first one works as it should. 但是,只有第一个实例在单击时会触发,当我单击第二个“微调”链接时,什么也没有发生,第一个实例按预期工作。 If there is only one link shown on a page then it works fine. 如果页面上仅显示一个链接,则它工作正常。

Any ideas? 有任何想法吗?

You're binding to an ID, and an ID can only exist once in the DOM. 您绑定到一个ID,并且一个ID在DOM中只能存在一次。 Try changing it to the class: 尝试将其更改为类:

<div class="nudgeHolder641">
    <a class="nudge" data-wl_id="641" data-nudge_for="415" data-nudge_from="63" href="#">Nudge</a>
</div>

<div class="nudgeHolder1172">
    <a class="nudge" data-wl_id="1172" data-nudge_for="415" data-nudge_from="63" href="#">Nudge</a>
</div>

And then bind using: 然后使用以下方式绑定:

$(function(){
$(".nudge").click(function() {

    var nudge_from = $( this ).data( 'nudge_from' );
    var nudge_for = $( this ).data( 'nudge_for' );
    var wl_id = $( this ).data( 'wl_id' );

    var dataString = 'nudge_from='+ nudge_from + '&nudge_for=' + nudge_for + '&wl_id=' + wl_id;

    $.ajax({
        type: "POST",
        url: "/pages/includes/ajax/nudge.php",
        data: dataString,
        success: function() {

            $('.nudgeHolder'+wl_id).html("<h3>Fantastic!</h3>")
            .append("<p>Nudge Sent!</p>")
            .hide()
            .fadeIn(1500, function() {
              //$('#message').append("<img id='checkmark' src='/images/icons/check.png' />");
            });
        }
    });
return false;
});

}); });

Rather than having unique div classes, use them as ID on a tag. 与其使用唯一的div类,不如将它们用作标签上a ID。 Like this: 像这样:

<div class="nudge">
    <a id="nudgeHolder641" data-wl_id="641" data-nudge_for="415" data-nudge_from="63" href="#">Nudge</a>
</div>

<div class="nudge">
    <a id="nudgeHolder1172" data-wl_id="1172" data-nudge_for="415" data-nudge_from="63" href="#">Nudge</a>
</div>

And then, in jQuery; 然后,在jQuery中; you need: 你需要:

$(document).ready(function(){
    $(".nudge a").click(function() {

Edit 编辑

Didn't notice that you were using $('#nudge') everywhere. 没注意到您到处都在使用$('#nudge') As ryadavilli pointed out; 正如ryadavilli所指出的; replace $('#nudge') to $(this) . $('#nudge')替换$('#nudge') $(this)

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

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