


How to load content from remote in Bootstrap modal window? Remote method introduction
This article will introduce you to Bootstrap Modal uses remote to load content remotely. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
Bootstrap’s Modal modal window component is still very useful, but during the development process, most of the content in the modal window is loaded from the backend. . To realize that the content of the modal window is loaded from the backend, there are two commonly used implementation methods. They are:
(1) There is an Iframe in Modal, and the remote content is loaded through the src of the Iframe. The disadvantage of this method is that the width and height of the modal box are difficult to adjust, and setting the width and height to fixed values destroys the responsive layout of bootstrap.
(2) Use Modal’s remote parameter to load remote content. There are some minor bugs in this method (the solution will be introduced later), but this method does not have the trouble of manually setting the width and height of the previous method.
Personally, I prefer the second method, so I will introduce the method of using remote. [Related recommendations: "bootstrap tutorial"]
The version of bootsrap used this time is 3.3.7
1. Page preparation
(1) Main page
Here on the main page, first place a modal box, but the content in the modal box is blank. The data after subsequent remote loading will be automatically filled in the p class="modal-content". Prepare the following html code:
<!-- 弹出模态窗口--> <div class="modal fade" style="top:13%;" tabindex="-1" role="dialog" id="showModal"> <div class="modal-dialog" role="document"> <div class="modal-content"> <!-- 内容会加载到这里 --> </div> </div><!-- /.modal-content --> </div><!-- /.modal-dialog --> </div><!-- /.modal -->
After placing the modal window, we can put a button on the main page to trigger the display of the modal window. The html code of this button is as follows:
<button>新增用户</button>
After the button and modal window are ready, we need to bind a click event to this button. After clicking, the modal window will be displayed and data will be loaded remotely. The js code is as follows:
$("#addBtn").click(function(){ // 打开模态框 $("#showModal").modal({ backdrop: 'static', // 点击空白不关闭 keyboard: false, // 按键盘esc也不会关闭 remote: '/sys/toAddUser' // 从远程加载内容的地址 }); });
This is the content of the main page. Note: I have not posted the code related to introducing bootstrap at the beginning. You need to introduce it yourself when using it.
(2) Preparation of the page to be loaded into the modal box
First of all, let me say that there is no need to introduce or add anything to this page. js and css. Because after this page is loaded into the modal box, it is equivalent to a part of the main page. It feels a bit like the main page dynamically importing it. It can access any content on the main page. This page can be regarded as the p content of class="modal-content". After loading, these HTML codes are embedded into it. Therefore, when writing this page, we can go to the bootstrap official website and copy the code of a modal box, and extract the content inside, which is most suitable for our remote page. The code I prepared is as follows:
<div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button> <h4 id="添加用户">添加用户</h4> </div> <div class="modal-body"> <form id="addForm"> <div class="form-group"> <label for="account">账号</label> <input type="text" class="form-control" id="account" name="account" placeholder="账号(用于登录)"> </div> <div class="form-group"> <label for="username">用户名</label> <input type="text" class="form-control" id="username" name="username" placeholder="用户名"> </div> <div class="form-group"> <label for="password">密码</label> <input type="password" class="form-control" id="password" name="password" placeholder="Password"> </div> </form> </div> <div class="modal-footer"> <button type="button" id="resetBtn" class="btn btn-default" >重置</button> <button type="button" id="saveBtn" class="btn btn-primary">提交</button> </div>
2. Introduction to the background
In fact, there is no introduction to the background code. When the button on the main page is clicked, the background receives this request , return the prepared page to the past and it will be ok. Use SpringMvc to implement it in one go. So I won’t introduce it.
3. Final effect
By clicking the new button on the main page, the modal box pops up and the remote page is loaded into the remote modal box.
4. Solve the minor bugs
(1) After testing, it was found that the content of this modal window is duplicated After loading once in the background, the modal window will not be loaded from the background again when it is closed and reopened.
(2) The content in this modal box will remain on the main page after loading, and the main page can be accessed directly. This is prone to problems, for example: there is a DOM element on the homepage with the same ID as the DOM element in the modal box, which is prone to bugs. We hope that the content in the modal window will be cleared directly after the modal window is closed. Lose.
The solution to the above two bugs is shown in the js code below. It actually monitors the closing of the modal window.
// 每次隐藏时,清除数据,确保不会和主页dom元素冲突。确保点击时,重新加载。 $("#showModal").on("hidden.bs.modal", function() { // 这个#showModal是模态框的id $(this).removeData("bs.modal"); $(this).find(".modal-content").children().remove(); });
5. Notes
It should be noted that the page loaded remotely is actually a small piece of html code, which does not require separate introduction of js and css (such as bootstrap's js and css). This is completely different from using Iframe. The content in Iframe can be regarded as a separate page, so you need to introduce js and css yourself when using Iframe.
For more programming-related knowledge, please visit: Programming Teaching! !
The above is the detailed content of How to load content from remote in Bootstrap modal window? Remote method introduction. For more information, please follow other related articles on the PHP Chinese website!

Bootstrap simplifies the development process mainly through its raster system, predefined components and JavaScript plug-ins. 1. The grid system allows for flexible layout, 2. Predefined components such as buttons and navigation bars simplify style design, 3. JavaScript plug-in enhances interactive functions and improves development efficiency.

Bootstrap is an open source front-end framework developed by Twitter, providing rich CSS and JavaScript components, simplifying the construction of responsive websites. 1) Its grid system is based on a 12-column layout, and the display of elements under different screen sizes is controlled through class names. 2) The component library includes buttons, navigation bars, etc., which are easy to customize and use. 3) The working principle depends on CSS and JavaScript files, and you need to pay attention to handling dependencies and style conflicts. 4) The usage examples show basic and advanced usage, emphasizing the importance of custom functionality. 5) Common errors include grid system calculation errors and style coverage, which require debugging using developer tools. 6) Performance optimization recommendations only introduce necessary components and customize samples using preprocessors

Bootstrap is an open source front-end framework developed by the Twitter team to simplify and speed up the web development process. 1.Bootstrap is based on HTML, CSS and JavaScript, and provides a wealth of components and tools for creating modern user interfaces. 2. Its core lies in responsive design, implementing various layouts and styles through predefined classes and components. 3.Bootstrap provides predefined UI components, such as navigation bars, buttons, forms, etc., which are easy to use and adjust. 4. Examples of usage include creating a simple navigation bar and advanced collapsible sidebar. 5. Common errors include version conflicts, CSS overwrites and JavaScript errors, which can be used through the version management tool.

Bootstrap can be integrated in React in two ways: 1) CSS and JavaScript files using Bootstrap; 2) Use the React-Bootstrap library. React-Bootstrap provides encapsulated React components, making using Bootstrap in React more natural and efficient.

There are two ways to use Bootstrap components in React projects: 1) CSS and JavaScript of the original Bootstrap; 2) Use libraries designed specifically for React such as react-bootstrap or reactstrap. 1) Install Bootstrap through npm and introduce its CSS file in the entry file, and then use the Bootstrap class name in the React component. 2) After installing react-bootstrap or reactstrap, directly use the React components it provides. Use these methods to quickly build a responsive UI, but pay attention to style loading and JavaScript

Bootstrap is an open source front-end framework that simplifies web development. 1. It is based on HTML, CSS, JavaScript, and provides predefined styles and components. 2. Use predefined classes and JavaScript plug-ins to implement responsive layout and interactive functions. 3. The basic usage is to introduce CSS and JavaScript files, use classes to create navigation bars, etc. 4. Advanced usage includes custom complex layouts. 5. Check the introduction of class names and files during debugging and use developer tools. 6. The optimization suggestion is to only introduce necessary files, use CDN, and use LESS or Sass when customizing.

How to create responsive web applications using Bootstrap and React? By combining Bootstrap's CSS framework and React's componentized architecture, modern, flexible and easy to maintain can be created. The specific steps include: 1) Importing the CSS file of Bootstrap and using its class to style React components; 2) Using React's componentization to manage state and logic; 3) Loading Bootstrap styles as needed to optimize performance; 4) Creating a dynamic interface using React's Hooks and Bootstrap's JavaScript components.

Bootstrap is an open source front-end framework that helps developers quickly build responsive websites. 1) It provides predefined styles and components such as grid systems and navigation bars. 2) Implement style and dynamic interaction through CSS and JavaScript files. 3) The basic usage is to introduce files and build pages with class names. 4) Advanced usage includes custom styles through Sass. 5) Frequently asked questions include style conflicts and JavaScript component issues, which can be solved through developer tools and modular management. 6) Performance optimization is recommended to selectively introduce modules and rationally use grid systems.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
