如何启用/禁用 jquery 中的按钮?

How to enable /disable buttons in jquery?

我的代码看起来像,

$("#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');;
        }
});

我的代码正确显示警报值,但没有使按钮 enable/disable 具有 DropdownSelectionValue 条件。作为初学者,我不知道该怎么做。请告诉我。

试试这个:-

$("#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
   }
});

您可以使用 jquery prop 相同

$('#Button1Id').prop('disabled', true);
$('#Button2Id').prop('disabled', false);

禁用

$('#Button1Id').attr('disabled','disabled');

启用

 $('#Button1Id').removeAttr('disabled');

来自 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/