jQuery中两个按钮的相同功能 [英] same function for two buttons in jQuery

查看:106
本文介绍了jQuery中两个按钮的相同功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个按钮: btnAdd btnUpdate 。我为按钮 btnUpdate 编写了一个jquery函数来验证网页中的某些字段,如:

I have two buttons: btnAdd and btnUpdate. I have written a jquery function for button btnUpdate to validate some fields in the web page like:

$(function() {

  $('#<%=btnUpdate.ClientID %>').click(function() {
    code here
    });
});

我想在 btnAdd 点击。所以我必须为btnAdd再次编写相同的函数。还有其他方法可以避免这种情况吗?

I want to do the same thing when btnAdd is clicked. So I have to write the same function again for btnAdd. Is there other way to avoid this?

(如果我写了一个javascript函数并在两个按钮的按钮点击事件中调用相同的函数,那很好。但有没有办法使用jQuery?)

(If I write one javascript function and call the same function in the button click event of both buttons, it's fine. But is there any way using jQuery?)

推荐答案

只需选择两个按钮,用逗号分隔:(#add,#update ).click ...
在你的代码中看起来像这样:

Just make a selection of two buttons, separated by a comma: ("#add, #update").click... Looks like this in your code:

$(function() { 

  $('#<%=btnUpdate.ClientID %>, #<%=btnAdd.ClientID %>').click(function() { 
    code here 
    }); 
}); 

这篇关于jQuery中两个按钮的相同功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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