如何在MVC Razor中动态禁用TextBox? [英] How to dynamically disable TextBox in MVC Razor?

查看:61
本文介绍了如何在MVC Razor中动态禁用TextBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想动态禁用/启用文本框,然后单击复选框.我该怎么做?我正在使用这个:

I want to dynamically disable/enable textbox aftre clicking the checkbox. How can I do it? I'm using this:

@{
    object addInput = (Model.AddInput) ? null : new { disabled = "disabled" };

 }

@Html.CheckBoxFor(model=> model.AddInput)

@Html.TextBoxFor(model => model.Input.Name, addInput)

,但仅在开始时有效.单击复选框不会更改任何内容.如何进行一些绑定来自动更改禁用状态?

but it works only on start. Clicking the checkbox isn't changing anything. How can do some binding to change disable state automaticaly?

推荐答案

这需要使用javascript完成.

This needs to be done in javascript.

@{
    object addInput = (Model.AddInput) ? null : new { disabled = "disabled" };

 }

@Html.CheckBoxFor(model=> model.AddInput)

@Html.TextBoxFor(model => model.Input.Name)


<script> 
    $('#AddInput').click(function() {
        var $this = $(this);

        if ($this.is(':checked')) {
            $('#Name').removeAttr("disabled"); 
        } else {
            $('#Name').attr("disabled", "disabled")
        }
    });
</script>

这篇关于如何在MVC Razor中动态禁用TextBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆