简体   繁体   中英

How to know that bootstrap plugin has been initialized

I'm trying to implement to validator.js plugin for bootstrap found here : https://1000hz.github.io/bootstrap-validator/

I want to hook into the submit event.

The following is written in the documentation for the plugin : "Be sure your submit handler is bound after the plugin has been initialized on your form"

I have written my submit handler like this and everything seems to work fine.

$( document ).ready(function() {
    $('#form').validator().on('submit', function (e) {
      if (e.isDefaultPrevented()) {
        console.log("invalid form");
      } else {
        console.log("everything looks good!");
      }
    })
});

But can I be sure that this will work all the time?

How can I be sure that the plugin has already been initialized?

A page can't be manipulated safely until the document is ready, because of:

$(document).ready(function(){...});

If you want load your functions after entire page (images or iframes) not just the DOM is ready use:

$(window).on("load", function(){...})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM