将onblur事件添加到ASP.Net MVC的Html.TextBox

jus*_*eve 4 javascript c# html-helper asp.net-mvc-2

HTML帮助程序(在MVC2中)的正确语法是什么,用于定义onblur处理程序,其中使用以下代码生成文本框:

<%=Html.TextBox(
    "ChooseOptions.AddCount" + order.ID,
    (order.Count > 0) ?  AddCount.ToString() : "",
    new { @class = "{number: true}  small-input" }
)
Run Code Online (Sandbox Code Playgroud)

Gab*_*abe 6

添加onblur到htmlattributes

<%=Html.TextBox(
    "ChooseOptions.AddCount" + order.ID,
    (order.Count > 0) ?  AddCount.ToString() : "",
    new { @class = "{number: true}  small-input", onblur = "alert('fired')" }
) %>
Run Code Online (Sandbox Code Playgroud)

或者更好的方法是使用jQuery添加它

$('#ChooseOptions_AddCount' + id).onblur(function() { alert('fired'); });
Run Code Online (Sandbox Code Playgroud)