我遇到了关于我的ajax请求的问题。 目标是创建一个包含多个行的表,每个行包括一个按钮或链接,可以单击该按钮或链接以发送值以更新数据库。完成此操作后,按钮或链接不再可单击,并由该特定行上的纯文本替换。
编辑:感谢@Sagar Arora,我们现在有了工作代码来发送特定行的特定变量值,但是我们需要一种方法来用纯文本替换按钮,这样就可以看到它已经被点击了。 / p>为了测试,我有以下potatoe.php:
// Several table-rows before and after
// Problem is, the class "paid_button" also belongs to the other buttons in every other
// table row, but I want to adress them specifically to get the id of that row for the ajax
<td><button name="paid" value="<?php echo $id; ?>" class="paid_button">Bezahlt</button></td>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="../js/global.js"></script>
在我的global.js
中$(document).on("click",".paid_button",function(){
var current_element = $(this);
$.ajax({
type: "POST",
url: "https://www.xxx",
data: { 'paid': current_element.attr("value")},
success: function(data){
alert("ok");
this.attr("disabled","disabled");//this will disable the clicked button
}
});
});
updatePotatoe.php:
// the update database query and execution (is already doing fine)
$request_id = $_POST['request_id'];
echo "test $request_id test";
我感谢任何帮助和提示!
答案 0 :(得分:1)
以下列方式:
<button name="paid" class="paid_button" value="$id" >Support</button>
$(document).on("click",".paid_button",function(){
var current_element = $(this);
$.ajax({
type: "POST",
url: "https://www.xxx.php",
data: { 'paid': current_element.attr("value")},
success: function(data){
alert("ok"); --> just for testing, I actually want to return values
this.attr("disabled","disabled");//this will disable the clicked button
}
});
});