Javascript 如何在MVC上将id设置为Html.TextBox项
我试图通过javascript捕获textbox元素,以便将文本放入其中。 那么如何设置文本框的idJavascript 如何在MVC上将id设置为Html.TextBox项,javascript,asp.net-mvc,razor,textbox,html.textbox,Javascript,Asp.net Mvc,Razor,Textbox,Html.textbox,我试图通过javascript捕获textbox元素,以便将文本放入其中。 那么如何设置文本框的id <tr> // Id to this textbox <td>@Html.TextBoxFor(item => item.OperationNo)</td> </tr> 它应该是属性名,即OperationNo 因此,您的JS将 document
<tr>
// Id to this textbox
<td>@Html.TextBoxFor(item => item.OperationNo)</td>
</tr>
它应该是属性名,即
OperationNo
因此,您的JS将
document.getElementById("OperationNo").html = " Some text " ;
您可以使用Chrome或JS中的web inspector查看页面上的html以查看元素属性。您可以像这样设置文本框的ID
@Html.TextBoxFor(item => item.OperationNo, new { id = "MyId" })
或
输出:
您可以使用以下代码来解决该问题:
function steVal() {
document.getElementById('txtPlace').value = "Set The Text";
}
@Html.TextBoxFor(m => m.OperationNo,new { @id = "txtPlace",@name="txtPlace" })
这是在web表单中使用具有相同属性的相同输入字段的场景中发生的。与单一视图/页面中的登录和注册表单类似。 以前是这样的 输入一个>
<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>
@Html.TextBoxFor(model=>model.Username,新的{@class=“form control”,placeholder=“Username”,“@required=“required”,autofocus=”“})
@Html.ValidationMessageFor(model=>model.Username,“,new{@class=“text danger”})
输入2>
<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>
@Html.TextBoxFor(model=>model.Username,新的{@class=“form control”,placeholder=“Username”,“@required=“required”,autofocus=”“})
@Html.ValidationMessageFor(model=>model.Username,“,new{@class=“text danger”})
然后我为用户名输入添加了一个唯一的id
新输入一个>
<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>
@Html.TextBoxFor(model=>model.Username,新{@class=“form control”,id=“loginUsername”,placeholder=“Username”,“@required=“required”,autofocus=”“})
@Html.ValidationMessageFor(model=>model.Username,“,new{@class=“text danger”})
新输入2>
<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>
@Html.TextBoxFor(model=>model.Username,新{@class=“form control”,id=“SignupUsername”,placeholder=“Username”,“@required=“required”,autofocus=”“})
@Html.ValidationMessageFor(model=>model.Username,“,new{@class=“text danger”})
这将覆盖与属性名称匹配的默认id。在匿名类中,属性名称前面不需要“@”符号。你唯一需要做的就是当属性名恰好是一个保留字时,比如“class”。我对此表示赞许,因为它展示了如何处理多个属性,这正是我所寻找的:o)
<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>
<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>
<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>