$("#like_answer_button").removeAttr("disabled");
$('#like_answer_button').click(function(e)
{
var id = $(this).closest('.box').data('id');
var url = "https://www.sdkwf.de/jquery/good_answer.php?answer_user=" + id;
var val = parseInt($("#like_answer_button").val(), 10);
$.post(url,{op:"<?php echo $ask; ?>"},function(data)
{
$("#status").html("");
val = val+1;
$("#like_answer_button").val(val);
$("#like_answer_button").attr("disabled", "disabled");
$("#like_answer_button").css("background-image","url(https://www.sdkwf.de/img/icon.png)");
})
});如果在PHP (while)中使用此代码对许多不同的按钮重复此脚本,您知道如何让此脚本工作吗?
<div class="box" data-id="'.$id.'">
<input type="submit" class="like" name="like_answer_button" value="10" id="like_answer_button" />它只适用于第一个按钮/输入,但其他按钮/输入不起作用。我猜是因为like_answer_button是为第一个预留的吧?
发布于 2018-02-06 00:17:20
您已经在为closest('.box')调用使用$(this)。这是对单击的like_answer_button的引用。在var中捕获它,并在post回调中使用它:
$("input[type='submit'].like").click(function(e)
{
var button = $(this);
var id = button.closest('.box').data('id');
var url = "https://www.sdkwf.de/jquery/good_answer.php?answer_user=" + id;
var val = parseInt(button.val(), 10);
$.post(url, { op: "<?php echo $ask; ?>" }, function(data)
{
$("#status").html("");
val = val + 1;
button.val(val);
button.attr("disabled", "disabled");
button.css("background-image","url(https://www.sdkwf.de/img/icon.png)");
})
});发布于 2018-02-06 00:09:52
HTML -不要对输入重复相同的id。如果需要,请确保名称和if是唯一的。
<div class="box" data-id="'.$id.'">
<input type="submit" class="like" value="10" />
<div class="box" data-id="'.$id.'">
<input type="submit" class="like" value="11" />
<div class="box" data-id="'.$id.'">
<input type="submit" class="like" value="12" />JS -在javascript中使用类选择器而不是特定的元素id
$('.like').click(function(e)
{
var id = $(this).closest('.box').data('id');
var url = "https://www.sdkwf.de/jquery/good_answer.php?answer_user=" + id;
var val = parseInt($(this).val(), 10);
$.post(url,{op:"<?php echo $ask; ?>"},function(data)
{
$("#status").html("");
val = val+1;
$(this).val(val);
$(this).attr("disabled", "disabled");
$(this).css("background-image","url(https://www.sdkwf.de/img/icon.png)");
})
});发布于 2018-02-06 00:27:24
// click handler
jQuery('.like').on('click', function(e){
// get the value of the input
var val = jQuery(e.currentTarget).val();
// find the element that contains the data-id and do something to it
jQuery('[data-id="'+ val +'"]').hide();
});<?php $iterator = 0; ?>
<?php foreach($array as $key=>$value): ?>
<div class="box" data-id="<?php echo 'target-'.$iterator; ?>">
<input type="submit" class="like" value="<?php echo 'target-'.$iterator; ?>" />
<?php $iterator++; ?>
<?php endforeach; ?>
https://stackoverflow.com/questions/48626391
复制相似问题