MVC DropDownList的 - 捕获更改事件不附加事件处理程序 [英] mvc dropdownlist - capturing change event without attaching the event handler

查看:113
本文介绍了MVC DropDownList的 - 捕获更改事件不附加事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以附着变更事件MVC到html.dropdownlist如下图所示:

I know I can attach change event to html.dropdownlist in mvc as shown below:

$('#ddList').change(function() {
            var value = $(this).val();


        });

不过,是有办法,我能做到这一点更像

However, is there a way I can do it more like

 <%=Html.DropDownList("ddList", Model.dropDown, new { @class = "Ddl", change = "ddListChange"  })%>

下面

js函数:

js function below:

function ddListChange() {
        alert("test");
    }

此外,是pferred一个比另一个更接近更$ P $?

Also, is one more approach more preferred than the other?

推荐答案

是的,你很接近,为的 的onchange 属性是你要找的人。什么

Yes, you were close, as the onchange attribute was what you were looking for.

&LT;%= Html.DropDownList(ddList,Model.dropDown,新{@class =DDL的onchange =ddListChange})%&GT;

jQuery的跳过中的事件,例如它的方法/参数事件名称的一部分。点击(...)/ .bind('点击',..)等于的onClick 属性。

jQuery skips the on part of the event names in its methods/parameters for events, e.g. .click(...) / .bind('click',..) equals the onClick attribute.

绑定的事件处理程序是preferred方式。就像你的造型分开成独立的CSS文件,你分开脚本到您自己的脚本文件。这使得HTML文件是更清洁和更容易阅读,除了分离prensentation的关切,无论你的脚本在做什么。这也使得你更容易页正常降级(即使未启用脚本的工作)。

Binding event handlers using javascript itself is the preferred way. Just like you separate styling into separate CSS files, you separate scripting into your own scripting files. This allows the HTML file to be cleaner and easier to read, in addition to separating the concerns of prensentation and whatever your scripts is doing. It also makes it easier for you page to degrade gracefully (work even if scripts aren't enabled).

这里是一篇文章讨论问题

这篇关于MVC DropDownList的 - 捕获更改事件不附加事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆