本文介绍了如何隐藏一个字段,当用户在另一个领域.NET MVC 4剃刀输入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个场像假日,使得在下面给出,

  [StringLength(50)]
 [显示名称(Constants.DisplayName.HolidayDay)
 公共虚拟字符串HolidayDay {搞定;组; }

 公共虚拟enumHolidayDay enumHolidayDay
 {
      得到
      {
            返回(enumHolidayDay)Enum.Parse(typeof运算(enumHolidayDay),HolidayDay);
      }
      组
      {
            HolidayDay = value.ToString();
      }
 }

 公开枚举enumHolidayDay
{
    [说明(星期六)]
    星期六= 1,    [说明(星期日)]
    星期日= 2,
}

和我Holiday.cshtml文件以下

 < D​​IV CLASS =行>
            < D​​IV CLASS =COL-MD-3>
                <标签=级=控制标签>
                设置假期< /标签>< BR />
                     @ Html.EnumCheckBoxFor(M = GT; m.enumHolidayDay)
            < / DIV>
        < / DIV>
       < BR />        < D​​IV CLASS =行>
            < D​​IV CLASS =COL-MD-3>
                <标签=级=控制标签>
                    白天< /标签>< BR />
                @ Html.TextBoxFor(型号=> model.HolidayDay,新{@class =表格控
                                              })DisableIf(()=> Model.IsReadOnly == true)而
            < / DIV>
        < / DIV>

在我的屏幕上有一个名为周六两个复选框,另一个是星期天。和一个文本框命名为一天,但用户可以在它的任何一个输入数据。一个是强制性的。如何处理,即如何禁用日场,当用户点击任何复选框。和假日领域仅用于这些控制。在这种情况下我必须处理它,而无需使用脚本,什么是code我需要添加这一点。任何人都可以请大家帮忙想办法了......


解决方案

检查波纹管code

与jQuery

\r
\r

$('#checkboxId')。点击(函数(){\r
    $(#txtholidy)切换(!this.checked);\r
});

\r

&LT;脚本SRC =htt​​ps://ajax.googleapis.com/ajax /libs/jquery/2.1.1/jquery.min.js\"></script>\r
&LT;输入类型=复选框ID =checkboxId检查='检查'&GT,周日\r
&LT; / BR&GT;&LT; / BR&GT;&LT;输入类型='文本'ID =txtholidy的风格=显示:无'&GT;

\r

\r
\r

更新
如果没有的jQuery

\r
\r

函数toggleControl()\r
{\r
\r
  如果(的document.getElementById(checkboxId)。检查==真)\r
    {\r
  。的document.getElementById(txtholidy)style.visibility =隐藏;\r
      }\r
其他\r
  {\r
  。的document.getElementById(txtholidy)style.visibility =看得见;\r
  }\r
  }\r
  

\r

&LT;输入类型检查='复选框'ID =checkboxId=检查的onClick =toggleControl()&GT;周日\r
&LT; / BR&GT;&LT; / BR&GT;&LT;输入类型='文本'ID =txtholidy的风格=visibility:hidden的&GT;

\r

\r
\r

I had one field something like Holiday such that is given below,

 [StringLength(50)]
 [DisplayName(Constants.DisplayName.HolidayDay)]
 public virtual string HolidayDay { get; set; }

And

 public virtual enumHolidayDay enumHolidayDay
 {
      get
      {
            return (enumHolidayDay)Enum.Parse(typeof(enumHolidayDay), HolidayDay);
      }
      set
      {
            HolidayDay = value.ToString();
      }
 }

And

public enum enumHolidayDay
{
    [Description("Saturday")]
    saturday = 1,

    [Description("Sunday")]
    sunday = 2,
}

And my Holiday.cshtml file is following

       <div class="row">
            <div class="col-md-3">
                <label for="" class="control-label">
                Set Holiday For </label><br />
                     @Html.EnumCheckBoxFor(m => m.enumHolidayDay)
            </div>
        </div>
       <br/>

        <div class="row">
            <div class="col-md-3">
                <label for="" class="control-label">
                    Day</label><br />
                @Html.TextBoxFor(model => model.HolidayDay, new { @class = "form-control"  
                                              }).DisableIf(() => Model.IsReadOnly == true)
            </div>
        </div> 

On my screen there are two checkboxes named Saturday and another one is Sunday. And one textbox named Day, But user can enter data in either one of it. One is mandatory. How to handle them, i.e. How to disable the Day field when the user click the any checkbox. And the Holiday field only used to these controls. In which event i have to handle it without using scripts and what are the code i need to add for this. Can anyone please help to find the solution...

解决方案

Check bellow code

With jQUery

$('#checkboxId').click(function () {
    $("#txtholidy").toggle(!this.checked);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='checkbox' id="checkboxId" checked='checked'>Sunday
</br></br><input type='textbox' id="txtholidy" style='display:none' >

UpdatedWithout jQUery

function toggleControl()
{

  if(document.getElementById("checkboxId").checked == true)
    {
  document.getElementById("txtholidy").style.visibility="hidden";
      }
else
  {
  document.getElementById("txtholidy").style.visibility="visible";
  }
  }
  
<input type='checkbox' id="checkboxId" checked='checked' onClick="toggleControl()">Sunday
</br></br><input type='textbox' id="txtholidy" style="visibility:hidden" >

这篇关于如何隐藏一个字段,当用户在另一个领域.NET MVC 4剃刀输入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 11:48