gpt4 book ai didi

asp.net-mvc - 如何在 MVC Razor 中动态禁用 TextBox?

转载 作者:行者123 更新时间:2023-12-02 11:01:53 25 4
gpt4 key购买 nike

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

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

}

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

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

但它仅在启动时有效。单击该复选框不会改变任何内容。如何进行一些绑定(bind)以自动更改禁用状态?

最佳答案

这需要在 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>

关于asp.net-mvc - 如何在 MVC Razor 中动态禁用 TextBox?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14683420/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com