jQuery detect click on disabled submit button

后端 未结 10 947
日久生厌
日久生厌 2020-12-03 06:20

Fiddle: http://jsfiddle.net/ugzux/

As you can see, I have a form with a disabled (via javascript) submit button.

I want to be able to bind a click event to

10条回答
  •  暖寄归人
    2020-12-03 07:08

    I have a slight more complicate but similar use case to you:

    1. There are many submit button on the screen, but by default they are disabled
    2. User need to click on the consent checkbox for button to be enabled
    3. The consent checkbox can be toggled on / off
    4. After click on the checkbox, they can click on all of the submit button

    Using jQuery, I have done it with Overlapping the button with a div, and wrapping the div instead another div

    See the below demo

    var toggleNeedConsent = function() {
      var isEnableCheckbox = $('#consent-confirm-checkbox').prop('checked');
      $('.needConsentButton').prop('disabled', !isEnableCheckbox);
      if (!isEnableCheckbox) {
        if (!$(".needConsentButtonOutterDiv").length) {
          $('.needConsentButton').wrap("
    "); } $('.needConsentButton').after('
    '); $('.needConsentButtonDiv').click(function(e) { alert('Please accept the consent first'); }) } else { $(".needConsentButtonDiv").remove(); } } $(function() { toggleNeedConsent(); $('#consent-confirm-checkbox').click(function() { toggleNeedConsent(); }); $("#approveButton").click(function() { alert('You are a wizard, Harry!'); }) $("#approve2Button").click(function() { alert('Harry, you are a wizard!'); }) })
    
    Click consent checkbox before approve! 
    



    Good things about this approach:

    • Only need to add a class for the button to be disabled, the other HTML remain the same
    • Can consent multiple button at once
    • The button is actually disabled instead of using fancy customized method, that make external library understand and make CSS accordingly

提交回复
热议问题