[英]Add onblur event to ASP.Net MVC's Html.TextBox
HTML幫助程序(在MVC2中)的正確語法是什么,用於定義onblur處理程序,其中使用以下代碼生成文本框:
<%=Html.TextBox(
"ChooseOptions.AddCount" + order.ID,
(order.Count > 0) ? AddCount.ToString() : "",
new { @class = "{number: true} small-input" }
)
將onblur
添加到htmlattributes
<%=Html.TextBox(
"ChooseOptions.AddCount" + order.ID,
(order.Count > 0) ? AddCount.ToString() : "",
new { @class = "{number: true} small-input", onblur = "alert('fired')" }
) %>
或者更好的方法是使用jQuery添加它
$('#ChooseOptions_AddCount' + id).onblur(function() { alert('fired'); });
我想提交一個解決方案,讓您可以重用您的功能代碼,如果您願意的話。 您可以在其他位置定義該函數,然后從HTML元素中調用它。
在myView.cshtml文件中的某個位置(可能位於body標簽的頂部),您可以定義腳本並在其中定義myFunc,如下所示:
<script>
function myFunc() {
alert('fired');
}
</script>
然后從HTML元素中調用它,如下所示:
<%=Html.TextBox(
"ChooseOptions.AddCount" + order.ID,
(order.Count > 0) ? AddCount.ToString() : "",
new { @class = "{number: true} small-input", onblur = "myFunc()" }
) %>
或者,如果您樂意使用HTML幫助程序來定義文本框,則可以這樣做
@Html.TextBoxFor(m => m.ChooseOptions.AddCount,
(order.Count > 0) ? AddCount.ToString() : "",
new { @class = "{number: true} small-input", onblur = "myFunc()" }
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.