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

Kav*_* P. 5 model-view-controller jquery razor

我的代码看起来像,

$("#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');;
        }
});
Run Code Online (Sandbox Code Playgroud)

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

mal*_*kam 6

来自 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.
Run Code Online (Sandbox Code Playgroud)

禁用:

$('#Button1Id').prop('disabled', true);
Run Code Online (Sandbox Code Playgroud)

启用:

$('#Button2Id').prop('disabled', false);
Run Code Online (Sandbox Code Playgroud)

http://api.jquery.com/prop/


Kar*_*sla 5

尝试这个 :-

$("#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
   }
});
Run Code Online (Sandbox Code Playgroud)