[英]ASP.NET MVC EditorFor custom javascript
The following statement: 以下声明:
<%= Html.EditorFor(model => model.Material.Serial) %>
Generates the following code; 生成以下代码;
<div class="editor-field">
<input type="text" value="" name="Material.Serial" id="Material_Serial" class="input-validation-error">
<span id="Material_Serial_validationMessage" class="field-validation-error">Debe ingresar un serial</span>
</div>
I want to add an onkeypress
javascript attribute to my input through the EditorFor
statement, I have tried to do the following: 我想通过
EditorFor
语句向我的输入添加onkeypress
javascript属性,我尝试执行以下操作:
<%= Html.EditorFor(model => model.Material.Serial, new{ onkeypress = "return disableEnterKey(event)"})%>
But this doesn't work. 但这不起作用。
Its difficult to pass Additional attributes to EditorFor templates, but alternatively the same functionality could be implemented using jQuery or something similar: 很难将附加属性传递给EditorFor模板,但也可以使用jQuery或类似的东西实现相同的功能:
$('#Material_Serial').keypress(function(event) {
disableEnterKey(event);
});
Use the following .... note the last part in the code. 使用以下....注意代码中的最后一部分。
@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control", @onchange="alert('Hello World')" } })
HTML generated for the above will be 为上面生成的HTML将是
<input class="form-control text-box single-line" data-val="true" data-val-length="The field Title must be a string with a maximum length of 255." data-val-length-max="255" data-val-required="The Title field is required." id="Title" name="Title" onchange="alert('Hello World')" type="text" value="" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.