Javascript 在ajax表单中单击后更改按钮
我在index.php中有以下代码:Javascript 在ajax表单中单击后更改按钮,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我在index.php中有以下代码: <table class = "table table-bordered table-responsive "> <thead> <tr> <th>Segment</th> <th>Action</th> </tr> </thead> <t
<table class = "table table-bordered table-responsive ">
<thead>
<tr>
<th>Segment</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach($data as $fetch): ?>
<tr>
<input type="hidden" name="user_id" value="<?php echo $_SESSION['user_id']?>">
<td class="segment" contenteditable="true"><?= $fetch['segment'] ?></td>
<td class="text-center">
<button class = "btn btn-default action-btn" data-id="<?= $fetch['id'] ?>" data-action="update">
<span class = "glyphicon glyphicon-edit"></span> Update
</button>
|
<button class = "btn btn-success activate-btn action-btn" data-id="<?= $fetch['id'] ?>" id="<?= $fetch['id'] ?>" type="submit" data-action="activate">
<span class = "glyphicon glyphicon-check"></span> Activate
</button>
<button style="display:none" class = "btn btn-danger deactivate-btn action-btn " data-id="<?= $fetch['id'] ?>" id="<?= $fetch['id'] ?>" data-action="deactivate">
<span class = "glyphicon glyphicon-folder-close"></span> deactivate
</button>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
段
行动
看来
<button ... id="<?= $fetch['id'] ?>"...Activate</button>
<button ... id="<?= $fetch['id'] ?>" ... deactivate</button>
id必须是唯一的。您正在重复激活和停用按钮中的id=“。您获得的相同id太多,这导致了问题。此外,您可以删除类名show函数,因为这将导致显示所有按钮,如果您想要切换功能,这是不可取的
下面我包含了一个代码部分,显示了相同的代码,共有2行和不同的ID
$(文档)。在(“单击”,“激活btn”,函数(){
var id=$(this.attr(“数据id”);
$(this.hide();
$(“#”+id+“-deactivate”).show();
});
$(文档)。在(“单击”,“停用btn”,函数()上){
$(this.show();
$(“#”+id+“-activate”、“.activate btn”).show();
});代码>
段
行动
段1
更新
|
激活
使停止工作
段2
更新
|
激活
使停止工作
在停用btn
单击功能中,您认为从何处获取id
来源?从var id?我说的对吗?如果你有解决方案,可以给我写代码吗?谢谢你@CarstenLøvboAndersenWell你的var id
在另一个点击函数中,因此由于它不是全局的,所以不能从另一个点击函数中这样调用它。我想知道它是如何工作的请帮助我,谢谢你@CarstenLøvboAndersen@CarstenL就在我前面一点点
// check if button is disabled
if($(this).attr("disabled") == "disabled"){
$(this).removeAttr("disabled")
}
else{
$(this).attr("disabled","disabled")
}
<button ... id="<?= $fetch['id'] ?>"...Activate</button>
<button ... id="<?= $fetch['id'] ?>" ... deactivate</button>
<button ... id="activate<?= $fetch['id'] ?>"...Activate</button>
<button ... id="deactivate<?= $fetch['id'] ?>" ... deactivate</button>