0

我正在构建一个MVC 4应用程序,如果输入了另一个TextBoxFor,我正在尝试禁用 一个TextBoxFor(或添加一些文本)。试图使用 switch-case 语句,但到目前为止没有运气。因为我必须添加文本/禁用其他TextBoxFor这必须在运行时运行

创建视图代码的一部分:

<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>

和这个:

@{ 
   switch (id1)
   {
     case 1:    
         <text> text1 </text>
         break;
     case 2:    
         <text> text2 </text>
         break;
     default:
         <text> text </text>
         break;
   }
}
4

1 回答 1

2

您可以使用一些 jquery 来检测文本框是否具有值,否则将其禁用。

$('input:text').filter(function () { return $.trim($(this).val()) == ''; })
               .attr("disabled", true);

如果您想在用户输入一个值之后(即在运行时)执行此操作,则将该语句放在文本框的“on change”事件中。

$('input:text]').on('change', function ()
{
    $('input:text').filter(function () { return $.trim($(this).val()) == ''; })
                   .attr("disabled", true);
});
于 2014-03-03T13:48:51.607 回答