asp.net-mvc 如何在 MVC 上将 id 设置为 Html.TextBox 项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16969262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to set id to Html.TextBox item on MVC
提问by Fadi Alkadi
Im trying to catch the textbox element by javascript, in order to put text into it. so how to set id to the textbox ??
我试图通过 javascript 捕获文本框元素,以便将文本放入其中。那么如何将 id 设置为文本框?
<tr>
// Id to this textbox
<td>@Html.TextBoxFor(item => item.OperationNo)</td>
</tr>
and then to put text into it by JS
然后通过JS将文本放入其中
//
document.getElementById("Textbox id").Text= " Some text " ;
回答by Win
You can set ID of a textbox like this.
您可以像这样设置文本框的 ID。
@Html.TextBoxFor(item => item.OperationNo, new { id = "MyId" })
OR
或者
@Html.TextBoxFor(item => item.OperationNo, htmlAttributes: new { id = "MyId" })
Output:
输出:
<input ... id="MyId" name="OperationNo" type="text" />
回答by Jay Shukla
You can use below code for resolve that problem:
您可以使用以下代码来解决该问题:
function steVal() {
document.getElementById('txtPlace').value = "Set The Text";
}
@Html.TextBoxFor(m => m.OperationNo,new { @id = "txtPlace",@name="txtPlace" })
回答by JeffB
It should be the property name, which is OperationNo.
它应该是属性名称,即OperationNo.
So your JS will be
所以你的JS将是
document.getElementById("OperationNo").html = " Some text " ;
You can use the web inspector in Chrome or JS to view the html on your page to see the element attributes.
您可以使用 Chrome 或 JS 中的 web 检查器查看页面上的 html 以查看元素属性。
回答by Nahom Tesfaye
This is happens in a scenario of using same input fields with same properties in web form. Like the case of Login and Signup forms in single view/page. Previously it was like this
这是在 Web 表单中使用具有相同属性的相同输入字段的情况下发生的。就像在单个视图/页面中登录和注册表单的情况一样。以前是这样的
input one >
输入一个 >
<div class="field-wrap">
@Html.TextBoxFor(model => model.Username, new { @class = "form-control",placeholder = "Username", @required = "required", autofocus = "" })
@Html.ValidationMessageFor(model => model.Username, "", new { @class = "text-danger" })
</div>
input two>
输入二>
<div class="field-wrap">
@Html.TextBoxFor(model => model.Username, new { @class = "form-control", placeholder = "Username", @required = "required", autofocus = "" })
@Html.ValidationMessageFor(model => model.Username, "", new { @class = "text-danger" })
</div>
then i added a unique id # for the username input
然后我为用户名输入添加了一个唯一的 id #
the new input one >
新输入一 >
<div class="field-wrap">
@Html.TextBoxFor(model => model.Username, new { @class = "form-control", id = "loginUsername", placeholder = "Username", @required = "required", autofocus = "" })
@Html.ValidationMessageFor(model => model.Username, "", new { @class = "text-danger" })
</div>
the new input two >
新输入二 >
<div class="field-wrap">
@Html.TextBoxFor(model => model.Username, new { @class = "form-control", id = "SignupUsername" , placeholder = "Username", @required = "required", autofocus = "" })
@Html.ValidationMessageFor(model => model.Username, "", new { @class = "text-danger" })
</div>

