简体   繁体   English

单选按钮禁用Html.textbox

[英]Radio button disable Html.textbox

I have 2 radio buttons Billable "Y" & Billable "N". 我有2个单选按钮,分别为“ Y”和“ N”。 They are connected to a database of values "Y" or "N". 它们连接到值“ Y”或“ N”的数据库。

If Radio = "Y" then TextBox NonBillableReason needs to be disabled. 如果Radio =“ Y”,则需要禁用TextBox NonBillableReason。

If Radio = "N" then TextBox NonBillableReason needs to be disabled. 如果Radio =“ N”,则需要禁用TextBox NonBillableReason。

The js. 的js。 that I have works fine on page load I get the proper behavior depending on the Radio value "Y" or "N". 我在页面加载时效果很好,根据Radio值“ Y”或“ N”,我得到了正确的行为。 The problem is I cannot seem to click Radio buttons and change the disabled, true (or) disabled, 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>

Since you are using jQuery, you can add a change event on the radio buttons and change disable the NonBillableReason element after that: 由于您使用的是jQuery,因此可以在单选按钮上添加一个change事件,并在NonBillableReason之后更改禁用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);
});

try 尝试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM