Disabled button still listens to click event

前端 未结 8 2546
感动是毒
感动是毒 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:

    <span class="btn btn-sm btn-primary btn-move-forward">Book</span>
    

    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()
    });
    
    0 讨论(0)
  • 2021-02-19 05:13

    you can try a porperty of almost tag: onclick="return false"

    0 讨论(0)
  • 2021-02-19 05:19

    The disabled property only applies to form elements. This means that unless the .btn-move-forward element is a <button> or <input type="button"> then the disabled attribute will have no effect.

    You can see a working example using a button here:

    $('.btn-move-forward').prop("disabled", true).click(function() {
      console.log('Moving forward...');
    })
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <button class="btn-move-forward">Move forward</button>

    0 讨论(0)
  • 2021-02-19 05:21

    Change it to use prop() instead.

    $('.btn-move-forward').prop("disabled", true)
    
    0 讨论(0)
  • 2021-02-19 05:21

    use pointer-events:none; where you don't needed click events

    .btn[disabled]{pointer-events:none;}
    

    document.querySelectorAll(".btn").forEach(btn=>{
        btn.addEventListener("click", e=>{
          alert(e.target.innerText)
        })
    })
    .btn{
      display:inline-block;
      background: #3f51b5;
      color: #fff;
      box-shadow: 0 2px 2px 0 rgba(0,0,0,.14),
       0 3px 1px -2px rgba(0,0,0,.2),
       0 1px 5px 0 rgba(0,0,0,.12);
        border: none;
        border-radius: 2px;
        position: relative;
        height: 36px;
        margin: 10px;
        min-width: 64px;
        padding: 0 16px;
        display: inline-block;
        font-family: "Roboto","Helvetica","Arial",sans-serif;
        font-size: 14px;
        font-weight: 500;
        text-transform: uppercase;
        letter-spacing: 0;
        overflow: hidden;
        will-change: box-shadow;
        transition: box-shadow .2s cubic-bezier(.4,0,1,1),background-color .2s cubic-bezier(.4,0,.2,1),color .2s cubic-bezier(.4,0,.2,1);
        outline: none;
        cursor: pointer;
        text-decoration: none;
        text-align: center;
        line-height: 36px;
        vertical-align: middle;   
    }
    
    .btn[disabled]{pointer-events:none; opacity:0.5}
    <div class="btn">Clickable button</div>
    <div class="btn">Clickable 2 button</div>
    <div class="btn">Clickable 3 button</div>
    <hr />
    <div class="btn" disabled>Disabled button</div>
    <div class="btn" disabled>Disabled 2 button</div>
    <div class="btn" disabled>Disabled 3 button</div>

    0 讨论(0)
  • 2021-02-19 05:25

    to disable:

    $('.btn-move-forward').prop("disabled", true);
    

    to re-enable:

    $('.btn-move-forward').removeClass('disabled'); // do this also.
    $('.btn-move-forward').prop("disabled", false);
    
    0 讨论(0)
提交回复
热议问题