且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在MVC 3剃须刀中向按钮添加事件处理程序

更新时间:2023-02-14 19:57:22

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

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

例如:id为btnSubmit
的提交按钮的jquery事件处理程序
 < script    类型  ="  >>  


(文档).ready(function(){


("class =" code-string>"


(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>