我的问题是,当数据值更改为Delivered
时,我无法显示或隐藏按钮。
这是我的php代码
<?php
$cardstatus = $row['cardStatus'];
if($cardstatus == NULL){
echo '<span class="badge badge-pill badge-warning">Card Pending</span>';
} elseif($cardstatus == 'Delivered') {
echo '<span class="badge badge-pill badge-success">Delivered</span>';
//Display Done Button
echo '<script>document.getElementById("done").style.display = "block";</script>';
//Undisplay Close Button
echo '<script>document.getElementById("cancel").style.display = "none";</script>';
} else {
echo '<span class="badge badge-pill badge-info">In Process</span>';
}
?>
“我的按钮”
//Button Cancel
<button class="btn btn-warning btn-sm cancel" data-id="<?php echo $row['id'] ?>" data-user="<?php echo $row['user'] ?>" id="cancel">Cancel</button>
//Button Done
<button class="btn btn-success btn-sm done" style="display: none;" id="done">Done</button>
我的Ajax
action: function () {
$.ajax({
type: "POST",
url: "accept.php",
data: {
addInfo: addInfo,
passInfo: passInfo,
id:id
},
dataType: "text",
success: function (data) {
// window.location.replace("purchased-top-up-cards.php");
$('#done').show(); //This will show but will be gone after refresh
$('#close').hide(); //This will hide but will show after refresh
},
error: function (err) {
console.log(err);
}
});
}
答案 0 :(得分:1)
信息太少。我希望您有一个可以执行以下操作的php:
<?php
$cardstatus = $row['cardStatus'];
if($cardstatus == NULL){
echo '{ "type":"warning" , "text":"Card Pending" }';
} elseif($cardstatus == 'Delivered') {
echo '{ "type":"success" , "text":"Delivered" }';
} else {
echo '{ "type":"info" , "text":"In Process" }';
}
?>
并在客户端上
$(function() {
$("#someForm").on("submit",function(e) {
e.preventDefault(); // cancel form
$.post("accept.php",{ "addInfo": addInfo, "passInfo":"passInfo","id":id },function(res) {
$("#someContainer").append('<span class="badge badge-pill badge-'+res.status+'>'+res.text+'</span>');
$("#done").toggle(res.status=="success");
$("#cancel").toggle(res.status!="success");
});
});
});
答案 1 :(得分:1)
var = test ? 1 : 0
被称为Ternary Operator
,与if/then/else
大致相同
在下面的代码中,将简单测试$cardstatus == 'Delivered'
用作三元运算符的条件,然后根据存储在变量中的值回显相应的值。
echo ( $cardstatus == 'Delivered' ? 'block' : 'none' )
更加简洁
if( $cardstatus == 'Delivered' ) { echo 'block'; } else { echo 'none'; }
因此,将相同的逻辑内联应用于HTML元素,但将取消按钮的block/none
倒转应该起作用。
<button class="btn btn-success btn-sm done" style="display:<?php echo ( $cardstatus == 'Delivered' ? 'block' : 'none' )?>;" id="done">Done</button>
<button class="btn btn-warning btn-sm cancel" style="display:<?php echo ( $cardstatus == 'Delivered' ? 'none' : 'block' )?>;" data-id="<?php echo $row['id'] ?>" data-user="<?php echo $row['user'] ?>" id="cancel">Cancel</button>
希望有帮助...