【问题标题】:Make alert window when clicking a disabled button单击禁用按钮时生成警报窗口
【发布时间】:2015-07-06 23:36:33
【问题描述】:

我有一个按钮默认禁用,但是当检查复选框时,然后启用按钮。我想编辑脚本以在禁用时单击按钮时发出警报窗口,以确保用户知道他/她必须选中复选框。

到目前为止我的脚本:

<script type="text/javascript">
$('#terms').on('change', function(){
          if ($(this).is(':checked')){
             $('#customButton').removeProp('disabled');
          }
          else{
             $('#customButton').attr('disabled', 'disabled');
          }
       });
</script>

<form id="payment-form" action="chargeCard.php" method="POST" name="payment-form">
<input type="checkbox" id="terms" />
<input type="image"  src="button3.png" id="customButton" value="submit" alt="button" disabled="disabled"/>
</form>

【问题讨论】:

标签: javascript html forms button checkbox


【解决方案1】:

我做了一个Fiddle

代码如下:

HTML

<form id="payment-form" action="chargeCard.php" method="POST" name="payment-form">
<input type="checkbox" id="terms" />
<input type="button"  id="customButton" value="submit" alt="button" class="disabled"/>
</form>

JS

$('#terms').on('change', function(){
if ($(this).is(':checked')){
    $('#customButton').removeClass('disabled');
}
else{
    $('#customButton').addClass('disabled');
}
});

$('#customButton').on('click',function(e){
if( $(this).hasClass('disabled') ){
    e.preventDefault();
    alert('Please confirm . . .');
}else{
    alert('go ahead...');
};
});

总而言之,如果一个元素被“禁用”,那么在它上面发生的所有事件都会被抑制。在我的小提琴中,我使用一种样式“伪造”了一个禁用的按钮,因此点击事件对浏览器仍然可见。

【讨论】:

  • 非常感谢,我编辑了它,但它让我上路了。
【解决方案2】:
$("#customButton").click(function(){
if !($("#terms").is(':checked'))
 {
 alert("Accept terms first!");
 }
})

如果您的浏览器无法检测到对已禁用按钮的点击,请在 div 内扭曲按钮并更改功能以点击该 div。

【讨论】:

  • 这对我有帮助。我没有想到将禁用的按钮包装在 div 中以捕获点击事件。
猜你喜欢
  • 1970-01-01
  • 2018-05-04
  • 2016-07-19
  • 2016-09-14
  • 2011-11-15
  • 2013-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多