【问题标题】:Change text field to grayed out with condition根据条件将文本字段更改为灰色
【发布时间】:2014-04-05 12:29:32
【问题描述】:

我使用通过 MVC5 应用程序创建的以下代码,并且我有一个名为 Type 的字段,它是下拉列表,我需要的是当您当前通过默认其 Dev 更改为 Prod 时,用户和密码框将变灰(下拉列表字段 - 类型)用户和密码字段更改为启用,我应该怎么做?

public class Ad
{
public int ID { get; set; }
public string Name { get; set; }

public string User { get; set; }

public string Password { get; set; }

public IEnumerable<SelectListItem> Type
{
get
{
return new[]
{
new SelectListItem {Value = "D", Text = "Dev"},
new SelectListItem {Value = "p", Text = "Production"}
};
}
}

创建生成的代码

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>


@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Admin</h4>
        <hr />
        @Html.ValidationSummary(true)

        <div class="form-group">
            @Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Name)
                @Html.ValidationMessageFor(model => model.Name)
            </div>
        </div>

        <

        <div class="form-group">
            @Html.LabelFor(model => model.User, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.User)
                @Html.ValidationMessageFor(model => model.User)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Password, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Password)
                @Html.ValidationMessageFor(model => model.Password)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Type, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(model => model.Type, Model.Type)

            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

使用脚本更新的代码

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

<script type="text/javascript">

    $('select[name="Type"]').change(function () {

        if ($(this).val() === 'Production')
        {

            $('input[name="User"]').prop("disabled",true);
            $('input[name="Password"]').prop("disabled",true);
        }

        else
        {
            $('input[name="User"]').prop("disabled",false);
            $('input[name="Password"]').prop("disabled",false);
        }

    });
</script>


@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Admin</h4>
        <hr />
        @Html.ValidationSummary(true)

        <div class="form-group">
            @Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Name)
                @Html.ValidationMessageFor(model => model.Name)
            </div>
        </div>

        <

        <div class="form-group">
            @Html.LabelFor(model => model.User, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.User)
                @Html.ValidationMessageFor(model => model.User)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Password, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Password)
                @Html.ValidationMessageFor(model => model.Password)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Type, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(model => model.Type, Model.Type)

            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

【问题讨论】:

    标签: c# asp.net asp.net-mvc asp.net-mvc-3 razor


    【解决方案1】:

    你需要像这样使用 jquery 来做到这一点:

    <script type="text/javascript">
    
        $(document).ready(function(){
    
        $('select[name="Type"]').change(function(){
    
        if($(this).val() === 'prod')
        {
    
            $('input[name="User"]').prop("disabled",true);
            $('input[name="Password"]').prop("disabled",true);
        }
    
            else
            {
            $('input[name="User"]').prop("disabled",false);
            $('input[name="Password"]').prop("disabled",false);
            }
    
        });
    });
    
    </script>
    

    这里是Fiddle DEMO

    【讨论】:

    • 非常感谢,我应该把这段代码放在哪里?你能把它添加到我发布的代码中吗,我对这个主题很陌生......,提前谢谢!
    • 在 Html.BeginForm 之前的视图顶部包含它
    • 还要确保在主布局中包含 Jquery 脚本文件
    • 我已经在创建文件中添加了它,我需要为它添加一些标签吗? Jquery 脚本文件是什么意思?
    • 是的,将其添加到此标签中: 并将 jquery 代码放入此
    【解决方案2】:

    你可以这样使用:

    $('#dropDownElement').change(function(){
    
        if($(this).text() == "Production")
        {
           $('#passwordElement').prop('readonly',true);
           $('#userElement').prop('readonly',true);
        } 
        else
        {
           $('#passwordElement').prop('readonly',false);
           $('#userElement').prop('readonly',false);   
        }
    
     })
    

    如果prop('readonly',true); 不起作用,请尝试attr('disabled', 'disabled');

    【讨论】:

      猜你喜欢
      • 2021-10-31
      • 2021-01-09
      • 2012-02-03
      • 2022-07-21
      • 2013-07-22
      • 2018-06-19
      相关资源
      最近更新 更多