【问题标题】:How to enable /disable buttons in jquery?如何在 jquery 中启用/禁用按钮?
【发布时间】:2015-05-05 01:37:56
【问题描述】:
我的代码看起来像,
$("#OperationTypeId").change(function () {
var DropdownSelectionValue = $("#OperationTypeId :selected").val();
alert(DropdownSelectionValue );
if (DropdownSelectionValue == 3)
{
$("#Button1Id").attr('disabled');
$(" Button2Id").attr('enable');;
//$("#Button1Id").hide();
}
else {
$("#Button1Id").attr('enable');
$(" Button2Id").attr('disabled');;
}
});
我的代码正确显示警报值,但没有使按钮在 DropdownSelectionValue 的条件下启用/禁用。作为初学者,我不知道该怎么做。请告诉我。
【问题讨论】:
标签:
jquery
razor
model-view-controller
【解决方案1】:
试试这个:-
$("#OperationTypeId").change(function () {
var DropdownSelectionValue = $("#OperationTypeId :selected").val();
if (DropdownSelectionValue == 3) {
$("#Button1Id").attr('disabled','disabled'); //or $("#Button1Id").prop('disabled',true);
$("#Button2Id").removeAttr('disabled'); //Use '#' id selector
}
else {
$("#Button1Id").removeAttr('disabled'); // or $("#Button1Id").prop('disabled',false);
$("#Button2Id").attr('disabled','disabled'); //Use '#' id selector
}
});
【解决方案2】:
你可以使用 jquery prop 来做同样的事情
$('#Button1Id').prop('disabled', true);
$('#Button2Id').prop('disabled', false);
【解决方案3】:
禁用
$('#Button1Id').attr('disabled','disabled');
启用
$('#Button1Id').removeAttr('disabled');
【解决方案4】:
来自 jQuery 网站
Properties generally affect the dynamic state of a DOM element without changing the serialized HTML attribute.
Examples include the value property of input elements, the disabled property of inputs and buttons, or the checked property of a checkbox.
The .prop() method should be used to set disabled and checked instead of the .attr() method.
禁用:
$('#Button1Id').prop('disabled', true);
启用:
$('#Button2Id').prop('disabled', false);
http://api.jquery.com/prop/