3

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

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

 }

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

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

但它只适用于开始。单击复选框不会更改任何内容。如何进行一些绑定来自动更改禁用状态?

4

3 回答 3

8

这需要在 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>
于 2013-02-04T09:21:48.337 回答
0

在单击复选框时添加 jquery 操作并适当地设置状态。

于 2013-02-04T09:22:10.400 回答
0

如果您出于某种原因想要启用它,您还可以使用以下内容:

      $('#Name').attr("disabled", false);
于 2015-10-24T07:23:10.573 回答