gpt4 book ai didi

javascript - 仅第一次触发 'click'

转载 作者:行者123 更新时间:2023-11-28 12:38:40 25 4
gpt4 key购买 nike

我有一个页面,允许用户“插入”某人做某事

我有以下 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>

我有以下代码来操作点击:

$(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;
});
});

尽管单击时,只有链接的第一个实例才会触发,但当我单击第二个“微移”链接时,什么也没有发生,第一个实例按预期工作。如果一页上只显示一个链接,那么它就可以正常工作。

有什么想法吗?

最佳答案

您正在绑定(bind)到一个 ID,并且 ID 在 DOM 中只能存在一次。尝试将其更改为类:

<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>

然后使用以下方式绑定(bind):

$(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;
});

});

关于javascript - 仅第一次触发 'click',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14502465/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com