-1

我有一个用于进入的剃须刀显示器。在一种情况下,我希望用户能够填充文本框,在另一种情况下,我希望阻止用户填充它。我正在使用如下代码:

@Html.TextBoxFor(model => model.Goop, new { @class = "text-box", maxlength = 2, onfocus = "javascript:this.select();" })
if (Model.Review.ReviewType.Equals("M"))
{ 
    <script type="text/javascript">
        $(function () {
            $("#Goop").prop("disabled", true);
         });
    </script>
}

我尝试了几种方法,jQuery(上图),CSS attribs,javascript,ASP.NET ......但都有相同的问题:提交表单时,如果禁用 Goop 文本框,Goop 的值在模型为空。想法?提前致谢!

4

2 回答 2

2

也许没有 jQuery 就没有那么酷了,但是当我在我的应用程序中这样做时,我会做一些类似的事情

if (Model.Review.ReviewType.Equals("M"))
{ 
    @Html.DisplayFor(model => model.Goop)
    @Html.HiddenFor(model => model.Goop)
}
else
{
    @Html.TextBoxFor(model => model.Goop)
}
于 2012-08-15T19:09:59.260 回答
1

If a form element is disabled, it does not post a value. That's how it's supposed to work.

To work around this, you will need to do one of several things. You can enable the fields just before posting by intercepting the submit method. You can use a hidden field to store the data in addition to the disabled control. Or you can just assume the values on the controller side.

by the way, it should be .prop("disabled", "disabled"), which renders as disabled="disabled", that's standards compliant.

于 2012-08-15T18:52:38.543 回答