如何在js函数中捕获点击按钮

How to capture clicked button in js functions?

本文关键字:按钮 js 函数      更新时间:2023-09-26

我必须使用这样的按钮:

<input type='submit' id='submit' class='processAnimation'>
<input type='reset' id='reset' class='processAnimation'>

现在我有两个js函数。当ajax请求启动时调用第一个函数,当ajax请求完成时调用seconf函数。

this.showLoading = function () {
     backupsource = $('.processAnimation').attr('class');
     $('.processAnimation').removeAttr('class').addClass('disabled-btn processAnimation');
     $('.processAnimation').attr( 'backupsource', backupsource );
}
this.hideLoading = function () {        
    backupsource = $('.processAnimation').attr('backupsource');
    if( backupsource != undefined ) {
       $('.processAnimation').removeAttr('class').addClass( backupsource );
       $('.processAnimation').removeAttr('backupsource');
    }   
}

编辑:以上两个功能都在工作,移动花替换点击按钮。当请求完成时,按钮返回。问题是,当我点击一个按钮时,它会用移动的花朵替换所有按钮(class=procesAnimation)。

感谢

由于您还没有发布点击事件绑定,我将快速猜测您的选择器设置不正确或与另一个元素冲突。试试这样的东西:

$('input').click(function(){
     switch( $(this).attr('id') ){
        case 'submit' :
            ShowLoading();
            break;
        case 'reset' :
            HideLoading();
            break;
      }
 });

并将初始化这两个函数的语法更改为:

function ShowLoading(){
     //do your show loading procedure here
};
function HideLoading(){
    //do your hide loading procedure here
};

这是使用当前的代码

$('.processAnimation').click(function (){
   if($(this).attr('type')=='submit'){
      //submit has been clicked
   }else{
      //reset has been clicked
   }
});

但是看起来您应该真正使用ID,而不是类的

如果你有jQuery,它是简单的

$('#submit').click(showLoading)
$('#reset').click(hideLoading)

只是两种不同的事件绑定。

还是我错过了什么?:)