如何在Checkbox选中PHP时启用Onclick

问题描述 投票:0回答:1

我想启用我的按钮,一旦勾选了复选框,我最近搜索了很多但是所有解决方案的形式或输入标签,我有一个按钮

<button onclick= buttonOnClick(); onmouseover="mouseOver();" onmouseout="mouseOut();" >Click here <i id="rtb" class="fa fa-moly-square rtb"></i></button>

和复选框代码:

 <div class="page__toggle">
      <label class="toggle">
        <input class="toggle__input" type="checkbox" >
        <span class="toggle__label">
          <span class="toggle__text">Check Me!</span>
        </span>
      </label>
    </div>
  </div>
</div>
javascript
1个回答
1
投票

这里的方法是将Event listener添加到HTML element,以便您可以对用户与复选框进行交互时做出反应,然后您可以更新disabled元素中button属性的状态。

const checkbox = document.getElementById('checkbox');
const button = document.getElementById('button');

const toggleButtonState = function (event) {
  button.disabled = !event.target.checked;
}

checkbox.addEventListener('change', toggleButtonState)
<button type="button" id="button" disabled>A button</button>
<label>
  <input type="checkbox" id="checkbox"/>
  <small>Toggle Button</small>
</label>
© www.soinside.com 2019 - 2024. All rights reserved.