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

时间:2015-03-04 06:44:16

标签: jquery razor model-view-controller

我的代码看起来像,

$("#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条件启用/禁用按钮。作为初学者,我不知道该怎么做。请告诉我。

4 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分: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 :(得分:1)

禁用

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

使

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

答案 3 :(得分:0)

您可以将jquery prop用于相同的

$('#Button1Id').prop('disabled', true);
$('#Button2Id').prop('disabled', false);
相关问题