单选按钮禁用Html.textbox

时间:2015-03-04 00:13:21

标签: javascript c# jquery html asp.net

我有2个收音机按钮Billable" Y" &安培;可结算" N"。它们连接到价值数据库" Y"或" N"。

如果Radio =" Y"然后需要禁用TextBox NonBillableReason。

如果Radio =" N"然后需要禁用TextBox NonBillableReason。

js。我在页面加载时工作正常我得到了正确的行为取决于无线电值" Y"或" N"。问题是我似乎无法单击单选按钮并更改禁用,true(或)禁用,false。

<label><span>YES</span>@Html.RadioButtonFor(model => model.Billable, "Y")   </label>
<label><span>NO</span>@Html.RadioButtonFor(model => model.Billable, "N")

@Html.TextBoxFor(model => model.NonBillableReason,new {@class = "form-control", @placeholder = "NOT BILLABLE BECAUS..."})

<script>
    $(document).ready(function () {

    if ($("#Billable:checked").val() == "Y") {
        $("#NonBillableReason").attr("disabled", true);
    }

    if ($("#Billable:checked").val() == "N") {
        $("#NonBillableReason").attr("disabled", false);
    }
});
</script>

2 个答案:

答案 0 :(得分:2)

由于您使用的是jQuery,您可以在单选按钮上添加change事件,然后在此之后更改禁用NonBillableReason元素:

function validate() {
    if ($("#Billable:checked").val() == "Y") {
        $("#NonBillableReason").attr("disabled", true);
    }

    if ($("#Billable:checked").val() == "N") {
        $("#NonBillableReason").attr("disabled", false);
    }
}
$(document).ready(function () {
    validate();
    $('input').change(validate);
});

答案 1 :(得分:0)

 if($('#radio_button').is(':checked')) 
{ 
 $("#NonBillableReason").attr("disabled", true); 
 }
else 
{
   $("#NonBillableReason").attr("disabled", false); 
}