jQuery detect click on disabled submit button

后端 未结 10 852
日久生厌
日久生厌 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 06:57

    The best way I've found to do this is to use a "disabled" class to disable the button. You can then catch click events normally in jquery. If $(this).hasClass('disabled'), you do your 'jazzy indication' stuff, along with event.preventDefault(); Once the user has done their thing, you can removeClass('disabled') from the input[type="submit"] 'button'. Easy!

    0 讨论(0)
  • 2020-12-03 06:57

    An other workaround with combination of a required checkbox could be:

    <input type="checkbox" class="einwilligung" name="einwilligung" value="ja"/>
    <div class="einwilligung_hinweis">U need to check this box</div>
    <div class="button_outer">
            <input type="submit" name="submit" value="Senden" id="submitButton" disabled="disabled" class="nope btn" />
            <span class="button_overlay"></span>
     </div>
    

    CSS-Magic

    #submitButton{
    display:inline-block;
    color:#D00019;
    width:160px;
    z-index:30;
    position:absolute;
    display:block;
    top:0;
    left:0;
    height:30px;
    outline:none;
    }
    
    #submitButton.nope{
    z-index:10;
    color:#333;
    }
    
    .button_outer {
    width:162px;
    height:32px;
    z-index:50;
    position:relative;
    }
    
    span.button_overlay{
    display:block;
    height:30px;
    width:162px;
    position:absolute;
    top:0;
    left:0;
    background:#fff;
    opacity:0.3;
    filter: alpha(opacity=30);
    z-index:20;
    }
    
    .einweilligung_hinweis_error{
    color:red;
    }
    

    JQuery-Stuff

    (document).ready(function() {
    
      $('.einwilligung').click(function() {
        var buttonsChecked = $('.einwilligung:checked');
        if (buttonsChecked.length) {
          $('#submitButton').removeAttr('disabled');
          $('#submitButton').removeClass('nope');
          $('.einwilligung_hinweis').removeClass('einweilligung_hinweis_error');
        }
        else {
          $('#submitButton').attr('disabled', 'disabled');
          $('#submitButton').addClass('nope');
        }
      });
    
      $('.button_outer').click(function(){
         if($('#submitButton').hasClass('nope')){
              $('.einwilligung_hinweis').addClass('einweilligung_hinweis_error');
          }else{
              $('.einwilligung_hinweis').removeClass('einweilligung_hinweis_error');
          }
      });
    
    });
    

    Maybe not state of the art, but it works pretty well!

    • the checkbox needs to be checked for giving the submit button a higher z-index
    • if not, there is the button_overlay above, with a click event on it, for highlighting the message
    0 讨论(0)
  • 2020-12-03 07:04
    $(document).on('click', '.wrapper-of-disabled-button', function(){
      if ($(this).find('button.disabled').length > 0) {
        // Do your magic on the parent -> $(this)
      }
    });
    

    Here you go ;)

    0 讨论(0)
  • 2020-12-03 07:05

    Found this in this question -

    Firefox, and perhaps other browsers, disable DOM events on form fields that are disabled. Any event that starts at the disabled form field is completely canceled and does not propagate up the DOM tree. Correct me if I'm wrong, but if you click on the disabled button, the source of the event is the disabled button and the click event is completely wiped out. The browser literally doesn't know the button got clicked, nor does it pass the click event on. It's as if you are clicking on a black hole on the web page.

    I'd thought you might be able to 'fake' a click by wrapping the button in a div and firing the logic on the div's click event. But, as indicated above, the events on disabled elements do not seem to be bubbled up the DOM tree.

    0 讨论(0)
  • 2020-12-03 07:05

    css

    [disabled]{
    pointer-events:none;
    }
    

    html

    <span class="button"><input type="submit" disabled /></span>
    

    You can try this for button click

    $('.button').click(function(){
    // do your code here.
    });
    
    0 讨论(0)
  • 2020-12-03 07:07

    Just don't disable the button, but prevent submit of the form. Looks like you're trying to validate the form; when you let JS take over the submit action, and return false, the form won't be submit

    0 讨论(0)
提交回复
热议问题