猿问

MVC5 根据另一个文本框值动态禁用文本框

我有 MVC5 项目,有 2 个文本框字段(客户端 ID、文档 ID)和一个下拉列表。


我想根据文档 ID 值动态启用或禁用客户端 ID 文本框和下拉列表,例如,如果我在文档 ID 文本框中写了一些东西,我希望看到客户端 ID 文本框和下拉列表被禁用,如果我从文档 ID 文本框中删除文本 我希望看到启用的客户端 ID 文本框和下拉列表。


到目前为止,我已经进行了研究,老实说,无法找到确切的答案。我读了一些关于 jquery 的东西,但我在 javascript 方面很弱。


这是我的代码。我怎样才能达到预期的结果?


<span>@Html.DisplayNameFor(model => model.ClientId).ToString():</span>

@Html.TextBox("ClientId", Model.Count()>0? Model.FirstOrDefault().ClientId:"", new { @class = "inline" })


<span>@Html.DisplayNameFor(model=>model.DocumentIdentificationId).ToString():</span>

@Html.TextBox("DocumentIdentificationId", Model.Count() > 0 ? Model.FirstOrDefault().DocumentIdentificationId: "", new {@class = "inline" })


<span>@Html.DisplayNameFor(model => model.DocumentType).ToString():</span>

@Html.DropDownList("DocumentTypes", new SelectList(documentTypes, "DocType", "DocName"),"", new { @class = "inline" }) 


慕无忌1623718
浏览 119回答 1
1回答

FFIVE

您可以编写一个脚本来执行此操作。因为@Html.TextBox("ClientId", Model.Count()>0? Model.FirstOrDefault().ClientId:"", new { @class = "inline" })会生成一个 id = ClientId 的输入标签您可以根据需要使用 change 事件或 keyup 事件。$("#ClientId").keyup(function(){&nbsp; &nbsp;if($(this).val() != ""){&nbsp; &nbsp; &nbsp; $("#DocumentIdentificationId").attr("disabled", "disabled");&nbsp;&nbsp; &nbsp; &nbsp; $("#DocumentTypes").attr("disabled", "disabled");&nbsp;&nbsp; &nbsp;}else{&nbsp; &nbsp; &nbsp; $("#DocumentIdentificationId").prop("disabled", false);&nbsp;&nbsp; &nbsp; &nbsp; $("#DocumentTypes").prop("disabled", false);&nbsp;&nbsp; &nbsp;}})<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>&nbsp; &nbsp;<input type="text" id="ClientId" />&nbsp; &nbsp;<input type="text" id="DocumentIdentificationId" />&nbsp; &nbsp; <select id="DocumentTypes">&nbsp; &nbsp; &nbsp; &nbsp;<option>Test<option>&nbsp; &nbsp; </select>
随时随地看视频慕课网APP
我要回答