Disabled button still listens to click event

前端 未结 8 2568
感动是毒
感动是毒 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: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}
    Clickable button
    Clickable 2 button
    Clickable 3 button

    Disabled button
    Disabled 2 button
    Disabled 3 button

提交回复
热议问题