Disabled button still listens to click event

前端 未结 8 2604
感动是毒
感动是毒 2021-02-19 04:53

I have a problem in a form where I do some jquery validations. If a specific input field is not filled out, it should disable a \"step forward\" button by adding a disabled attr

8条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-19 05:02

    I think, disabling click event is not a good idea. Because you have to enable click event again, if you enable the button.

    For disabled button, I just check disabled state, if disabled return.


    Html button:

    Book
    

    How to disable button:

    $('.btn-move-forward').addClass('disabled');
    

    Click event:

    $('.btn-move-forward').on('click', function(){
      if($(this).hasClass('disabled')) { 
        console.log('-- Button is disabled. Return from here. --');
        return false;
      }
    
      // write code for click event here
      $('#step2, #step3').toggle()
    });
    

提交回复
热议问题