I am building an MVC 4 application and i am trying to disable one TextBoxFor (or add some text) if another TextBoxFor has been entered. Was trying to use switch-case statement but so far no luck. Because i have to add text/disable other TextBoxFor this has to be run in runtime.
Part of the Create View code:
<div class="editor-label2">
@Html.LabelFor(model => model.First)
</div>
div class="editor-field2">
@Html.TextBoxFor(model => model.First, new { @id = "id1"} )
@Html.ValidationMessageFor(model => model.First)
</div>
<div class="editor-label3">
@Html.LabelFor(model => model.Second)
</div>
<div class="editor-field3">
@Html.TextBoxFor(model => model.Second, new { @id = "id2"} )
@Html.ValidationMessageFor(model => model.Second)
</div>
and this:
@{
switch (id1)
{
case 1:
<text> text1 </text>
break;
case 2:
<text> text2 </text>
break;
default:
<text> text </text>
break;
}
}
You can use some jquery to detect if a textbox has a value and otherwise disable it.
$('input:text').filter(function () { return $.trim($(this).val()) == ''; })
.attr("disabled", true);
If you are wanting to do it after the user enters a value (i.e. at runtime) then put the statement inside the "on change" event of the textboxes.
$('input:text]').on('change', function ()
{
$('input:text').filter(function () { return $.trim($(this).val()) == ''; })
.attr("disabled", true);
});