![](/img/trans.png)
[英]ASP.NET MVC 5 @Html.EditorFor and javascript datepicker
[英]ASP.NET MVC EditorFor custom javascript
以下聲明:
<%= Html.EditorFor(model => model.Material.Serial) %>
生成以下代碼;
<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>
我想通過EditorFor
語句向我的輸入添加onkeypress
javascript屬性,我嘗試執行以下操作:
<%= Html.EditorFor(model => model.Material.Serial, new{ onkeypress = "return disableEnterKey(event)"})%>
但這不起作用。
很難將附加屬性傳遞給EditorFor模板,但也可以使用jQuery或類似的東西實現相同的功能:
$('#Material_Serial').keypress(function(event) {
disableEnterKey(event);
});
使用以下....注意代碼中的最后一部分。
@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control", @onchange="alert('Hello World')" } })
為上面生成的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.