How do you specify the primary and secondary actions for a Bootstrap modal window?

后端 未结 1 1325
孤街浪徒
孤街浪徒 2021-02-05 21:10

I\'m using Twitter\'s Bootstrap modal functionality. When someone clicks submit on my form, I\'m testing to see if they have checked any of the available options:



        
1条回答
  •  一向
    一向 (楼主)
    2021-02-05 22:06

    Well, the problem is that right now Bootstrap does not have any proper callbacks for their action buttons. Therefore, you have to do this in a roundabout way and create your own. Hope this helps!

    Here's a JS fiddle showing it work: http://jsfiddle.net/iwasrobbed/rYLWz/3/

    And here is the code:

    HTML File

    JS File

    $(document).ready(function() {
    
        // Verify if checkboxes were checked.
        // If they weren't, show a modal
        $('a.save-button').click(function() {
            if ($("input:checked").length === 0) {
    
                $('#my-modal').modal({
                    show: 'true',
                    backdrop: 'true',
                    keyboard: 'true'
                });
            } else {
                $('form').submit(); 
            }
    
            // prevent click jump
            return false;
        });
    
        // Let's attach a listener on form submission
        $('form').submit(function() {
            alert('We submitted the form!');
        });
    
        // Hide modal if "Go back" is pressed
        $('#my-modal .go-back-button').click(function() {
            $('#my-modal').modal('hide');
            alert('We went back to the form');
        });
    
        // Hide modal if "Okay" is pressed
        $('#my-modal .okay-button').click(function() {
            $('#my-modal').modal('hide');
            $('form').submit();
        });
    
    });
    

    I added a little CSS as well:

    ul.inputs-list li {
        margin-left: 10px;
    }
    .hidden {
        display: none   
    }
    

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