最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Add onblur event to ASP.Net MVC's Html.TextBox - Stack Overflow

programmeradmin2浏览0评论

What's the correct syntax for an HTML helper (in MVC2) to define an onblur handler where the textbox is generated with code like:

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

What's the correct syntax for an HTML helper (in MVC2) to define an onblur handler where the textbox is generated with code like:

<%=Html.TextBox(
    "ChooseOptions.AddCount" + order.ID,
    (order.Count > 0) ?  AddCount.ToString() : "",
    new { @class = "{number: true}  small-input" }
)
Share Improve this question edited Jul 7, 2014 at 21:15 abatishchev 100k88 gold badges301 silver badges442 bronze badges asked May 8, 2010 at 17:52 justStevejustSteve 5,52420 gold badges77 silver badges140 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

Add the onblur to htmlattributes

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

Or a better way add it with jQuery

$('#ChooseOptions_AddCount' + id).onblur(function() { alert('fired'); });

I wanted to submit a solution which allows you to reuse your function code, if you want. You can define the function elsewhere, and then call it from the HTML element.

Somewhere in the myView.cshtml file (maybe at the top of the body tags) you can define a script and define myFunc inside, like this:

<script>
    function myFunc() {
        alert('fired');
    }
</script>

Then call it from the HTML element, like this:

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

Or, if you're happy to use a HTML helper to define your textbox, you can do

@Html.TextBoxFor(m => m.ChooseOptions.AddCount,
    (order.Count > 0) ?  AddCount.ToString() : "",
    new { @class = "{number: true}  small-input", onblur = "myFunc()" }
)
发布评论

评论列表(0)

  1. 暂无评论