Display Bootstrap Modal using javascript onClick

后端 未结 4 1508
时光取名叫无心
时光取名叫无心 2021-02-03 20:48

I need to be able to open a Twitter bootstrap modal window using the onClick=\"\" or similar function. Just need the code to go into the onClick=\"\".

相关标签:
4条回答
  • 2021-02-03 21:18

    You don't need an onclick. Assuming you're using Bootstrap 3 Bootstrap 3 Documentation

    <div class="span4 proj-div" data-toggle="modal" data-target="#GSCCModal">Clickable content, graphics, whatever</div>
    
    <div id="GSCCModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
     <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;  </button>
            <h4 class="modal-title" id="myModalLabel">Modal title</h4>
          </div>
          <div class="modal-body">
            ...
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary">Save changes</button>
          </div>
        </div>
      </div>
    </div>
    

    If you're using Bootstrap 2, you'd follow the markup here: http://getbootstrap.com/2.3.2/javascript.html#modals

    0 讨论(0)
  • 2021-02-03 21:33

    I was looking for the onClick option to set the title and body of the modal based on the item in a list. T145's answer helped a lot, so I wanted to share how I used it.

    Make sure the tag containing the JavaScript function is of type text/javascript to avoid conflicts:

    <script type="text/javascript"> function showMyModalSetTitle(myTitle, myBodyHtml) {
    
       /*
        * '#myModayTitle' and '#myModalBody' refer to the 'id' of the HTML tags in
        * the modal HTML code that hold the title and body respectively. These id's
        * can be named anything, just make sure they are added as necessary.
        *
        */
    
       $('#myModalTitle').html(myTitle);
       $('#myModalBody').html(myBodyHtml);
    
       $('#myModal').modal('show');
    }</script>
    

    This function can now be called in the onClick method from inside an element such as a button:

    <button type="button" onClick="javascript:showMyModalSetTitle('Some Title', 'Some body txt')"> Click Me! </button>
    
    0 讨论(0)
  • 2021-02-03 21:33

    A JavaScript function must first be made that holds what you want to be done:

    function print() { console.log("Hello World!") }
    

    and then that function must be called in the onClick method from inside an element:

    <a onClick="print()"> ... </a>
    

    You can learn more about modal interactions directly from the Bootstrap 3 documentation found here: http://getbootstrap.com/javascript/#modals

    Your modal bind is also incorrect. It should be something like this, where "myModal" = ID of element:

    $('#myModal').modal(options)
    

    In other words, if you truly want to keep what you already have, put a "#" in front GSCCModal and see if that works.

    It is also not very wise to have an onClick bound to a div element; something like a button would be more suitable.

    Hope this helps!

    0 讨论(0)
  • 2021-02-03 21:41

    I had the same problem, after researching a lot, I finally built a js function to create modals dynamically based on my requirements. Using this function, you can create popups in one line such as:

    puyModal({title:'Test Title',heading:'Heading',message:'This is sample message.'})
    

    Or you can use other complex functionality such as iframes, video popups, etc.

    Find it on https://github.com/aybhalala/puymodals For demo, go to http://pateladitya.com/puymodals/

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