【发布时间】:2019-06-21 15:34:24
【问题描述】:
我正在使用 ajax 将按钮的 id 发送到 test.php,但我被困在如何在 ajax 过程中禁用按钮并显示处理图像 gif 的步骤上,
当我使用我的代码时,它只会禁用和隐藏第一个表格行按钮,而不是单击的特定按钮
我的代码是这样的
<tbody>
<?php
$letter=mysqli_query($con,"SELECT * FROM letters order by id DESC");
if(mysqli_num_rows($letter)>0){
while($rows_letter=mysqli_fetch_array($letter))
{
$id=$rows_letter['id'];
$subject=$rows_letter['subject'];
$status=$rows_letter['status'];
?>
<tr>
<th class="text-center" scope="row">1</th>
<td class="text-center"><?php echo $subject ;?></td>
<td class="text-center"><?php if($status == 1){echo '<mark style="background-color: #5cb85c; color:white;" > Successfully Sent </mark>'; }else{ echo '<mark style="background-color:#f0ad4e; color:white;"> Not Sent Yet </mark>';}?></td>
<td><button type="button" class="btn btn-info btn-sm btn-block">
<span class="fa fa-pencil-square-o"></span> Edit</button></td>
<td><button type="button" class="btn btn-danger btn-sm btn-block"> <span class="fa fa-trash-o"></span> Move To Trash</button></td>
<td>
<img src="https://svc.opushealth.com/balcoltrasavings/img/Processing.gif" id="img" style="display:none"/>
<button type="button" onclick="startsend(<?php echo $id;?>);"
id="id" value="<?php echo $id;?>"class="btn btn-success btn-sm btn-block">
<span class="fa fa-paper-plane-o"></span> Send To All</button></td>
</tr>
<?php
}
}
?>
</tbody>
<script type='text/javascript'>
//AJAX function
function startsend(id) {
$('#img').show();
$("#id").attr("disabled", true);
$.ajax({
type: "POST",
url: "test.php",
data:{ id: id },
success: function(msg){
alert( "Button Id is " + msg );
$('#img').hide();
}
});
}
</script>
test.php 文件是
<?php
if(isset($_POST['id'])){
$id = $_POST['id'];}
///rest process
?>
【问题讨论】: