最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Make alert window when clicking a disabled button - Stack Overflow

programmeradmin2浏览0评论

I have a button which by default is disable, but when a checkbox is checked the button is then enabled. I would like to edit the script to make an alert window when the button is clicked when disabled, to make sure the user knows he/she has to check the checkbox.

My script so far:

<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>

I have a button which by default is disable, but when a checkbox is checked the button is then enabled. I would like to edit the script to make an alert window when the button is clicked when disabled, to make sure the user knows he/she has to check the checkbox.

My script so far:

<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>
Share Improve this question asked Apr 28, 2015 at 8:17 Fulgut98Fulgut98 3231 gold badge2 silver badges12 bronze badges 4
  • You want to trigger event handler on the click of disabled button? – Satpal Commented Apr 28, 2015 at 8:19
  • I want to trigger a function which, if the button is disabled and clicked an alert window pops up. – Fulgut98 Commented Apr 28, 2015 at 8:21
  • You are missing the point, disabled element do not trigger events. Test it yourself jsfiddle/ym34ud8j – Satpal Commented Apr 28, 2015 at 8:26
  • you should check this question, stackoverflow./questions/3100319/event-on-a-disabled-input – Robin Commented Apr 28, 2015 at 8:27
Add a ment  | 

2 Answers 2

Reset to default 4

I've made a Fiddle

Here's the code:

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...');
};
});

The long and short of it is, if an element is 'disabled' all events that happen on it are suppressed. In my fiddle I've 'faked' a disabled button using a style so the click event is still visible to the browser.

$("#customButton").click(function(){
if !($("#terms").is(':checked'))
 {
 alert("Accept terms first!");
 }
})

if your browser can not detect click on a disabled button, Warp the button inside a div and change the function to click on that div.

发布评论

评论列表(0)

  1. 暂无评论