【问题标题】:Textbox become readonly base on dropdown selected value文本框根据下拉选择值变为只读
【发布时间】:2020-03-10 11:58:18
【问题描述】:

如何将文本框的属性更改为只读而不是隐藏?

下拉菜单:

<div class="form-group">
@Html.LabelFor(model => model.CIVSTAT, "Civil Status", htmlAttributes: new { @class = "control-label" })    
<select class="form-control" id="CIVSTAT" name="CIVSTAT">
<option> </option>
<option>Single</option>
 <option>Married</option>
<option>Widowed</option>
<option>Separated</option>
<option>Divorced</option>
</select>
@Html.ValidationMessageFor(model => model.CIVSTAT, "", new { @class = "text-danger" })
</div>

文本框:

 <div class="form-group">
    @Html.LabelFor(model => model.SPOUSE, "Spouse", htmlAttributes: new { @class = "control-label" })
    @Html.EditorFor(model => model.SPOUSE, new { htmlAttributes = new { @class = "form-control", @autocomplete = "off", placeholder = "Spouse's Name", maxlength = 40, @id="spouse" } })
    @Html.ValidationMessageFor (model => model.SPOUSE, "", new { @class = "text-danger" })
 </div>

JS代码:

  < script >
    $("#CIVSTAT").change(function () {
        if ($(this).val() == "Single") {
            $('#spouse').hide();                       
        }else {
            $('#spouse').show();               
        }
    });

【问题讨论】:

    标签: javascript onchange


    【解决方案1】:

    请尝试以下代码 sn-p。

    $("#CIVSTAT").change(function () {
        if ($(this).val() == "Single") {
            $('#spouse').attr('readonly', true);                       
        }else {
            $('#spouse').removeAttr('readonly');               
        }
    });
    

    【讨论】:

    • 我们真的应该在这里使用 DOM 道具,而且更简单。 $('#spouse').prop('readonly', $(this).val() === 'Single')
    猜你喜欢
    • 2021-07-01
    • 2014-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-03
    • 1970-01-01
    • 2015-05-26
    • 1970-01-01
    相关资源
    最近更新 更多