根据加载时的复选框选择启用文本框

时间:2014-01-21 07:07:27

标签: jquery razor

我有2个复选框,它控制启用或禁用文本框txtProductName。目前,如果在加载时选中复选框2,则txtProductName将被禁用,但是如果在加载时选中复选框1,则txtProductName仍然禁用它应该启用的位置

<input type="checkbox" id="Check1" value="Value1" onclick="selectOnlyThis(this.id)" style="margin:1em 1em 5px 5px" @(ViewBag.Status == "Yes" ? " checked" : "")/>Yes

<input type="checkbox" id="Check2" value="Value1" onclick="selectOnlyThis(this.id)" style="margin:1em 1em 5px 5px" @(ViewBag.Status == "No" ? " checked" : "")/>No


@Html.TextBoxFor(m => m.ProductName, new { id = "txtProductName", style = "width: 150px;margin:1.0em 1.5em 5px -10px;height:20px; font-size:12px", @Value = ViewBag.ProductName })



if ($('#Check1 :checked')) {
            $('#txtProductName').removeAttr("disabled");
}
if ($('#Check2 :checked')) {
            $('#txtProductName').attr('disabled', 'disabled').css("backgroundColor", "white");
}

2 个答案:

答案 0 :(得分:0)

试试这个,

if ($('#Check1').is(':checked')){

而不是

if ($('#Check1 :checked')) {

答案 1 :(得分:0)

使用

if ($('#Check1').is(':checked')){

if ($('#Check1').prop('checked')){

而不是

if ($('#Check1 :checked')) {