Hongmu Notes
Home Language Notes jquery+bootstrap implements an ajax to dynamically submit data and modify the page
Language Notes JavaScript

jquery+bootstrap implements an ajax to dynamically submit data and modify the page

jquery+bootstrap implements an ajax to dynamically submit data and modify the page

ad1a1b7183213903

That's it!

Click the button to open a pop-up window; after submitting the data, you will be returned to the edit page.

Implement code

<!-- 模态框 -->
<div class="modal fade" id="myModal" tabindex="-1" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <!-- 模态框头部 -->
      <div class="modal-header">
        <h4 class="modal-title" id="myModalLabel">提交表单</h4>
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
      </div>
      <!-- 模态框主体 -->
      <div class="modal-body">
        <!-- 表单 -->
        <form id="myForm">
          <div class="form-group">
            <label for="inputData">输入数据</label>
            <input type="text" class="form-control" id="inputData" name="inputData" placeholder="输入数据">
          </div>
        </form>
      </div>
      <!-- 模态框底部 -->
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
        <button type="button" class="btn btn-primary" id="submitBtn">提交</button>
      </div>
    </div>
  </div>
</div>

<!-- 按钮 -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
  打开模态框
</button>

<!-- jQuery 和 Ajax 代码 -->
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
  $(function() {
    // 在提交按钮被点击时发送 Ajax 请求
    $("#submitBtn").click(function() {
      $.ajax({
        url: "your_api_url",
        type: "POST",
        data: $("#myForm").serialize(),
        dataType: "json",
        success: function(response) {
          // 当请求成功时,更新输入框的值为返回的 JSON 数据中的 text 字段
          $("#inputData").val(response.text);
          // 关闭模态框
          $("#myModal").modal("hide");
        },
        error: function(xhr, status, error) {
          // 当请求失败时,修改输入框的值为 "抱歉"
          $("#inputData").val("抱歉");
        }
      });
    });
  });
</script>

In the above code, the modal dialog box contains a form that includes an input field. #inputDataIt will be updated, whether the outcome is successful or not.

When the modal dialog box is opened, users can enter data into the input field and then click the Submit button. #submitBtn Submit the form.

The submit button's click event is bound to an Ajax request; upon successful completion of the request, the input field's value is updated with the JSON data returned from the server. text Field; close the modal dialog box;

When the request fails, set the input field value to "Sorry". Note: Replace. your_api_url Your actual value.

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

js move the mouse up to modify the style attribute

js move the mouse up to modify the style attribute Language Notes JavaScript

When the mouse hovers over the element, the color changes to red; when the mouse leaves the element, the color changes to black. To retrieve an element by its ID: `document.getElementById()`; Event handler for mouse hover: `onmouseover`; Event handler for mouse leave: `onmouseout`.<!DOCTYPE html><html lang= "en">...
👁 130

Recommended reading

Responsive tea product display and sales website template 0349

Responsive tea product display and sales website template 0349 Practical Collection Yiyou template

This EyouCMS responsive template is ideal for the tea exhibition and sales industry. Its classical and elegant design is perfect for showcasing tea products, brand stories, tea culture, and sales information. It helps tea brands effectively present their products online and attract both tea enthusiasts and distributors. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS...
👁 35
(Adaptive mobile version) Heavy Industry Machinery & Equipment Website – PBootCMS Template; Download General Website Source Code for Crane and Lifting Machinery – 0410

(Adaptive mobile version) Heavy Industry Machinery & Equipment Website – PBootCMS Template; Download General Website Source Code for Crane and Lifting Machinery – 0410 Practical Collection pbootcms Template

An adaptive mobile website template for heavy-duty machinery and crane equipment, built with PbootCMS. The professional industrial design is ideal for showcasing lifting equipment, cranes, and related products as well as engineering applications. This template helps heavy machinery manufacturers showcase their brands online and attract industrial and construction clients. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn...
👁 37
Responsive office supplies service company website template 0350

Responsive office supplies service company website template 0350 Practical Collection Yiyou template

An eyouCMS responsive website template designed for office supplies and service companies. Its professional and practical design effectively showcases office supply products, corporate services, solutions, and brand identity. This template helps office supply businesses showcase their products online and attract corporate clients. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for YouyouCMS...
👁 46
(Adaptive mobile version) Metal products – Metal mesh filters and screens; PBootCMS website template; Download source code for a black-themed corporate website for metal manufacturing companies – 0411

(Adaptive mobile version) Metal products – Metal mesh filters and screens; PBootCMS website template; Download source code for a black-themed corporate website for metal manufacturing companies – 0411 Practical Collection pbootcms Template

This set includes an adaptive mobile-friendly metal products and metal mesh filter screen website template for the PbootCMS platform. The design features a sleek black aesthetic, making it ideal for showcasing metal products, industrial filters, and precision-machined components. It helps metal manufacturing enterprises demonstrate their manufacturing expertise online and attract industrial clients. Template Overview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn...
👁 40
Network Software Technology Company Website Template 0351

Network Software Technology Company Website Template 0351 Practical Collection Yiyou template

This EYO CMS template is ideal for web, software, and technology companies. Its modern, tech-oriented design effectively showcases software products, web services, technical capabilities, and corporate image. It helps technology firms present their brand online and attract business clients. Template Overview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EYO CMS | EYO CMS (Eyo...
👁 45
(Adaptive mobile phone version) Sheet metal processing equipment website pbootcms template Machinery manufacturing inspection website source code download 0412

(Adaptive mobile phone version) Sheet metal processing equipment website pbootcms template Machinery manufacturing inspection website source code download 0412 Practical Collection pbootcms Template

A mobile-adaptive PbootCMS website template for sheet metal processing equipment and machinery manufacturing inspection. The design style is professional and sophisticated, suitable for displaying sheet metal equipment, testing instruments and processing services. It helps machinery manufacturing companies showcase their technologies online and attract precision manufacturing customers. Template display Installation instructions Website backend:/admin.php Account: admin Password: admin Unzip password: www.4s5.cn Related...
👁 47