我尝试使用AJAX和jQuery更新活动/暂停按钮,用户点击"暂停" - >我必须将状态更改为"暂停" +活动按钮。
用户点击"激活"按钮 - >我将状态更改为"已激活" +暂停按钮。
这是HTML / PHP代码:
状态:
<?php
if($row['active']==2) // 2 = Suspended
{
?>
<span id="status<?php echo $row['id']; ?>">Suspended
<button type="button" class="active-button" value="<?php echo $row['id']; ?>">active</button>
</span>
<?php
}
else if($row['active']==1) // 1 = Activated
{
?>
<span id="status<?php echo $row['id']; ?>">Activated
<button type="button" class="suspend-button" value="<?php echo $row['id']; ?>">suspend</button>
</span>
<?php
}
?>
这是jQuery / AJAX代码:
$(function() {
$('.active-button').click(function(){
var add_active = 1;
var add_id = $(this).val();
var status = "#status"+add_id;
$(status).text('Loading...');
$.post('active.php',{id: add_id, active: add_active}, function(){
$(status).html("").append("Suspended <button type='button' class='active-button' value="+add_id+">active</button>");
});
});
$('.suspend-button').click(function(){
var add_active = 2;
var add_id = $(this).val();
var status = "#status"+add_id;
$(status).text('Loading...');
$.post('active.php',{id: add_id, active: add_active}, function(){
$(status).html("").append("Activated <button type='button' class='suspend-button' value="+add_id+">suspend</button>");
});
});
});
但问题是跨度&#34;状态&#34;不会改变(在数据库中它运作良好)。
我几乎可以肯定问题出现在这一行:
$.post('active.php',{id: add_id, active: add_active}, function(){
$(status).html("").append("Activated <button type='button' class='suspend-button' value="+add_id+">suspend</button>");
});
答案 0 :(得分:0)
更改此行
$(status).html("").append("Activated <button type='button' class='suspend-button' value="+add_id+">suspend</button>");
到
$(status).html("").append("Activated <button type='button' class='suspend-button' value='" + add_id + "'>suspend</button>");