如何在MVC 3剃须刀中向按钮添加事件处理程序 [英] How to add event handler to a button in mvc 3 razor

查看:899
本文介绍了如何在MVC 3剃须刀中向按钮添加事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MVC 3剃刀视图应用程序.我将一个提交按钮拖放到layout.cshtml中.现在我需要为其添加事件处理程序.我没有像在aspx中一样在属性窗口中看到任何选项.我该怎么做..

解决方案

提交按钮是HTML元素.因此,只能使用提交"按钮调用javascript.同样,单击带有表单的提交"按钮后,该页面会自动将页面提交到服务器.您只需要为表单指定action属性,此处就不需要javascript代码.

但是,如果您想添加自定义处理,则可以使用纯JavaScript或jQuery进行编码.

例如:id为btnSubmit
的提交按钮的jquery事件处理程序

 < script    类型  ="  >>  


(文档).ready(function(){


(""


(document).ready(function(){ span> #btnSubmit").click(function(e){ alert(" ); }); }); </ 脚本 >


I have a MVC 3 razor view application . I dragged and droped a submit button in to the layout.cshtml. now i need to add an event handler to it. i don''t see any option in the property window like before i did in aspx. how do i do it..

解决方案

Submit buttom is an HTML element. So, only javascript can be invoked with the submit button. Also, The Submit button with form automatically submits the page to server when clicked. You just have to specify action attribute for the form, no javascript code is needed here.

Yet, if you would like to add custom processing you can code either in pure javascript or jQuery.

ex:jquery event handler for submit button whose id=btnSubmit

<script type="text/javascript">


(document).ready(function () {


("#btnSubmit").click(function (e) { alert("submit"); }); }); </script>


这篇关于如何在MVC 3剃须刀中向按钮添加事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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