Javascript 启用和禁用按钮
我想使用jquery或javascript启用和禁用按钮。 真正的情况是 如果我单击start按钮,它将转到php表Column如果状态为running状态,则start按钮将被禁用。如果状态为stopped状态,则start按钮需要启用Javascript 启用和禁用按钮,javascript,jquery,Javascript,Jquery,我想使用jquery或javascript启用和禁用按钮。 真正的情况是 如果我单击start按钮,它将转到php表Column如果状态为running状态,则start按钮将被禁用。如果状态为stopped状态,则start按钮需要启用 jQuery('#button-id').attr('disabled', true); jQuery('#button-id').attr('disabled', false); 您只需更改已禁用的属性值请使用JQuery提供的Ajax $('#btn')
jQuery('#button-id').attr('disabled', true);
jQuery('#button-id').attr('disabled', false);
您只需更改已禁用的属性值请使用JQuery提供的Ajax
$('#btn').click(function (e) {
e.preventDefault()
let btn = $(this)
btn_disable(btn)
$.ajax({
url: 'your_url',
method: 'your_method',
data: {your_data}, //if you haven't data please type {}
success: function (response) {
btn_enable(btn)
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
btn_enable(btn)
},
complete: function (response) {
btn_enable(btn)
}
})
})
function btn_disable(btn) {
btn.attr('disabled', 'disabled')
btn.addClass('disabled')
}
function btn_enable(btn) {
btn.removeAttr('disabled')
btn.removeClass('disabled')
}
在这里,您可以阅读有关Ajax的更多信息:
请检查以下样本。如果API响应为true,则按钮被禁用;如果为false,则按钮被启用
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.ajax({url: "ur_Api, success: function(result){
if(result === true){
$("#btn_status").attr('disabled', true);
}
else {
$("#btn_status").attr('disabled', false);
}});
});
});
</script>
</head>
<body>
<div id="div1"><h2>Let jQuery AJAX Change This Text</h2></div>
<button id="btn_status">Status</button>
</body>
</html>
请发布您的努力提示:代码。