开发者

how to turn on/off the validation when the element is getting disabled/enabled

开发者 https://www.devze.com 2023-03-27 18:00 出处:网络
I want to disable the textbox if the checkbox is unchecked. View: @Html.CheckBoxFor(model => model.prop1, new { onchange = \"OnChange(this,\'prop2\')\" })

I want to disable the textbox if the checkbox is unchecked. View:

@Html.CheckBoxFor(model => model.prop1, new { onchange = "OnChange(this,'prop2')" })
...
@Html.TextBoxFor(model => model.prop2)

JavaScript handler

function OnChange(cb, id) 
    {
        if ($(开发者_如何学Pythoncb).is(':checked') == false)
        {
            $('input#' + id).val('');
            $('input#' + id).attr('disabled', 'disabled');
        }else {
            $('input#' + id).attr('disabled', '');
            $('input#' + id).change();
        }
}

But there is a problem with validation messages. If prop2 is for example marked as "[Required]" when the textbox is disabled error message should disappear. And when the user check the checkbox I should enable the textbox field and turn on the validation

What should I do for this?


Please check following questions:

Asp.net MVC 3 Conditional validation with DataAnnotations

MVC custom validation: compare two dates

0

精彩评论

暂无评论...
验证码 换一张
取 消