【问题标题】:Can't get event blur with asp.net mvc 3无法使用 asp.net mvc 3 获取事件模糊
【发布时间】:2012-07-24 02:07:04
【问题描述】:

大家好,我一直在努力如何从 asp.net mvc 3 强类型视图中获取事件模糊。 这是我尝试过的。

<table>
 <td class="contentTable">
     @Html.LabelFor(model => model.Ciudad)
  </td>
  <td>
     @Html.EditorFor(model => model.Ciudad, new{id= "mapSearch"})
     @Html.ValidationMessageFor(model => model.Ciudad)
 </td>
</table>


<script type="text/javascript">
    $(function () {
        $('#mapSearch').blur(function () {
           myFunction();
       });
});
</script>   

myFunction 已经是我正在使用的由按钮触发的函数,但现在我希望它由事件触发。我也试过:

@Html.EditorFor(model => model.Ciudad, new{onblur= "mapSearch();"})

谢谢

【问题讨论】:

  • 您好,您的代码的 html 输出是什么?
  • 我用它来获取谷歌地图的经纬度,所以当你写城市(ciudad)时,地图会模糊加载城市。 myFunction 是 geocode()
  • @Html.EditorFor(model =&gt; model.Ciudad, new{id= "mapSearch"}) 变成了什么? &lt;input type='text' 还是别的什么?

标签: c# javascript asp.net-mvc-3 events onblur


【解决方案1】:

试试这个。像这样更改您的视图标记

 @Html.EditorFor(model => model.Ciudad)

这样的脚本

$(function () {
    $('#Ciudad').blur(function () {
           myFunction();
    });
});

假设你有一个名为myFunction的javascript函数存在

function myFunction() {
        console.debug("myFunction Called");
       //Some other awesome stuff here
}

JsFiddle 示例:http://jsfiddle.net/C3wMY/4/

【讨论】:

  • 我实际上正在尝试,但没有工作。与您建议的更改结果相同。
  • @LeoJ:您是否遇到任何脚本错误?使用工作示例查看我的更新答案。
  • 不,我没有收到任何错误,我使用 firebug 进行调试,我看到您的示例运行良好,但我仍然无法猜测我做错了什么。我正在考虑放弃这个并使用一个按钮:/
【解决方案2】:

EditorFor 没有采用 html 属性的覆盖。在“EditorFor”级别,它不知道可能会插入哪些元素或如何获取它们的属性。您需要使用 TextBoxFor、DropDownListFor 或其他添加特定元素的助手。或者,如果您使用的是编辑器,则需要在编辑器模板中添加属性。如果需要,您可以将属性传递给模板,但作为模型(或 ViewBag 或其他方式)的一部分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-27
    • 2012-06-20
    • 1970-01-01
    相关资源
    最近更新 更多