javascript 按钮 onclick() 替代使用 jquery 或 mvc3 动作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8395620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 03:18:37  来源:igfitidea点击:

button onclick() alternative using jquery or mvc3 actions?

javascriptjqueryasp.net-mvc-3

提问by Mariah

Is there a better (more up to date) alternative to the following standard onbuttonclick() call to a javascript function using jquery or mvc3 helper actions?

对于使用 jquery 或 mvc3 辅助操作对 javascript 函数的以下标准 onbuttonclick() 调用,是否有更好的(更新的)替代方法?

<button onclick="javascript:AddNew()" title="ad action">Add</button> 

回答by Fiacc

using JQuery...

使用 JQuery...

<button id="buttonID" title="ad action">Add</button> 

$('#buttonID').click(function() {
  alert('click called.');
});

回答by Khairu Aqsara

when the button more than one we could add some class to them and just add some line of code to call them one by one, like

当按钮不止一个时,我们可以向它们添加一些类,只需添加一些代码行即可一一调用它们,例如

<button class="mybtn">I'm Clikced 1</button>
<button class="mybtn">I'm Clikced 2</button>
<button class="mybtn">I'm Clikced 3</button>
<button class="mybtn">I'm Clikced 4</button>

and Jquery look like this

和 Jquery 看起来像这样

$(function(){
  $("button.mybtn").click(function(){
     var value = $(this).text();
     alert(value);
  });
});

回答by Adam Rackis

For a particular button, use StressChicken's answer. For the more general case, where you want to handle any button belonging to a certain class—or matching any selector for that matter—see jQuery's onfunction. Note that this will also work with dynamically added content.

对于特定按钮,请使用 StressChicken 的答案。对于更一般的情况下,如果要处理属于某一类或匹配的任何选择的任何按钮就事论事看到jQuery的功能。请注意,这也适用于动态添加的内容。

$(document).on("click", ".selectorClass", function() {
  alert('click called.');
});

Note that this function requires jQuery 1.7

请注意,此功能需要 jQuery 1.7

回答by Jwalin Shah

Using JQuery it would be better.

使用 JQuery 会更好。

Add

添加

$('#button').click(function() { alert('Function called.'); });

$('#button').click(function() { alert('函数调用。'); });