html文件Js写输入框和弹框调接口jQuery

news2025/4/24 9:26:52

 业务场景:需要使用写一个html文件,实现输入数字,保存调接口。

1、使用 JS原生写法, fetchAPI调接口,使用 alert 方法弹框会阻塞线程,所以写了一个弹框。

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      /* 清空上下边距*/
      margin: 0;
      padding: 0;
    }

    .container {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 80vh;
    }

    .form-group {
      padding: 20px;
      border-radius: 10px;
      width: 200px;
      display: flex;
      flex-direction: column;
      align-items: center;
      /* background-color: lightblue; */
    }

    .name {
      width: 200px;
      text-align: left;
      font-size: 16px;
      font-weight: 400;
      font-family: MicrosoftYaHei;
    }

    input {
      margin-top: 10px;
      padding: 5px;
      font-size: 14px;
      width: 185px;
      outline: none;
      /* 去掉焦点边框 */
    }

    button {
      padding: 8px 15px;
      font-size: 16px;
      line-height: 16px;
      background-color: #0C52C3;
      color: white;
      border: none;
      border-radius: 6px;
      cursor: pointer;
      margin-top: 20px;
      &:hover {
        background-color: #5586d5;
      }
    }

    #tip-box {
      position: fixed;
      top: 15%;
      left: 50%;
      transform: translate(-50%, -50%);
      width: 400px;
      border: 1px solid #ccc;
      border-radius: 15px;
      box-shadow: 0 0 10px rgba(0, 0, 0, .2);
      padding: 10px 20px;
      background-color: #fff;
      z-index: 99999;
      text-align: left;
      animation: tip-show .3s forwards;
    }

    .tip-header {
      font-size: 16px;
      font-weight: bold;
      color: #333;
      padding: 9px 6px 3px;
    }

    .close-btn {
      float: right;
      margin: 10px;
    }

    /* .close-btn:hover {
      background-color: #ccc;
    } */

    .tip-content {
      font-size: 14px;
      color: #333;
      padding: 8px 6px;
    }

    @keyframes tip-show {
      0% {
        opacity: 0;
      }

      100% {
        opacity: 1;
      }
    }

    @keyframes tip-hide {
      0% {
        opacity: 1;
      }

      100% {
        opacity: 0;
      }
    }
  </style>
  <script src="jquery.min.js"></script>

</head>

<body>
  <div class="container">
    <div class="form-group">
      <div class="name">请输入累计热量(GJ)</div>
      <input type="number" name="" id="input" class="form-control" value="" min="0" step="0.01" required="required"
        title="" placeholder="0.00" required="required" onblur="handleInputChange(event)"
        onkeydown="handlekeyDown(event)" />
      <button class="btn btn-primary" onclick="handleButtonClick()">保存</button>
    </div>
    <div id="tip-box" style="display: none;" onclick="hideTip()">
      <div class="tip-header">提示</div>
      <div class="tip-content">提示内容</div>
      <button class="close-btn" onclick="hideTip()">确定</button>
    </div>
  </div>
  <script>
    let url = 'http://1x.1x.6.1xxx:12xxx';
    let api = '/base/xxxxxDXZZConfig';

    function handleInputChange(event) {
      event.target.value = Number(event.target.value).toFixed(2);
    }

    function handlekeyDown(event) {
      console.log(event);
      if (event.keyCode === 69) {
        event.preventDefault();
      }
    }

    function handleButtonClick() {
      let value = input.value;
      console.log(value);
      if (!value) {
        showTip('请输入有效的数值');
        return;
      }

      let data = { value };
      // 使用 URLSearchParams 将数据对象转换为查询字符串
      let params = new URLSearchParams(data).toString();
      let requestUrl = `${url}${api}?${params}`;
      let options = { method: "GET" }

      try {
        fetch(requestUrl, options)
          .then(res => res.json())
          .then(json => {
            console.log(json);

            if (json.code == '0') {
              showTip('保存成功');
            } else {
              showTip(json.msg);
            }

            input.value = null;
          })
          .catch(error => {
            showTip('请求失败,请稍后再试');
            console.error('Fetch Error:', error);
            input.value = null;
          });
      } catch (error) {
        showTip('请求失败,请稍后再试');
        console.error('Fetch Error:', error);
        input.value = null;
      }
    }

    var tipBox = document.querySelector('#tip-box');
    var closeBtn = tipBox.querySelector('.close-btn');

    function showTip(message) {
      tipBox.querySelector('.tip-content').innerHTML = message;
      tipBox.style.display = 'block';
    }

    function hideTip() {
      tipBox.style.animation = 'tip-hide .3s forwards';
      setTimeout(function () {
        tipBox.style.display = 'none';
        tipBox.style.animation = '';
      }, 300);
    }

  </script>
</body>

</html>

 2、使用jQuery,先 npm install jquery,然后引入

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>表底录入</title>
  <style>
    * {
      /* 清空上下边距*/
      margin: 0;
      padding: 0;
    }

    .container {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 80vh;
    }

    .form-group {
      padding: 20px;
      border-radius: 10px;
      width: 200px;
      display: flex;
      flex-direction: column;
      align-items: center;
      /* background-color: lightblue; */
    }

    .name {
      width: 200px;
      text-align: left;
      font-size: 16px;
      font-weight: 400;
      font-family: MicrosoftYaHei;
      color: #333;
    }

    input {
      margin-top: 10px;
      padding: 5px;
      font-size: 14px;
      width: 185px;
      outline: none;
      /* 去掉焦点边框 */
      color: #333;
    }

    button {
      padding: 8px 15px;
      font-size: 16px;
      line-height: 16px;
      background-color: #0C52C3;
      color: white;
      border: none;
      border-radius: 6px;
      cursor: pointer;
      margin-top: 20px;

      &:hover {
        background-color: #5586d5;
      }
    }

    #tip-box {
      position: fixed;
      top: 15%;
      left: 50%;
      transform: translate(-50%, -50%);
      width: 400px;
      border: 1px solid #ccc;
      border-radius: 15px;
      box-shadow: 0 0 10px rgba(0, 0, 0, .2);
      padding: 10px 20px;
      background-color: #fff;
      z-index: 99999;
      text-align: left;
      animation: tip-show .3s forwards;
    }

    .tip-header {
      font-size: 16px;
      font-weight: bold;
      color: #333;
      padding: 9px 6px 3px;
    }

    .close-btn {
      float: right;
      margin: 10px;
    }

    .tip-content {
      font-size: 14px;
      color: #333;
      padding: 8px 6px;
    }
  </style>
  <script src="./node_modules/jquery/dist/jquery.min.js"></script>

</head>

<body>
  <div class="container">
    <div class="form-group">
      <div class="name">请输入累计热量(GJ)</div>
      <input type="number" name="" id="input" class="form-control" value="" min="0" step="0.01" required="required"
        title="" placeholder="0.00" required="required" onblur="handleInputChange(event)"
        onkeydown="handlekeyDown(event)" />
      <button class="btn btn-submit">保存</button>
    </div>
    <div id="tip-box" style="display: none;">
      <div class="tip-header">提示</div>
      <div class="tip-content">提示内容</div>
      <button class="close-btn">确定</button>
    </div>
  </div>
  <script>
    let url = 'http://1x.1x.6.1xxx:12xxx';
    let api = '/base/xxxxxDXZZConfig';

    function handleInputChange(event) {
      event.target.value = Number(event.target.value).toFixed(2);
    }
    function handlekeyDown(event) {
      // 阻止键盘输入e的情况
      if (event.keyCode === 69) {
        event.preventDefault();
      }
    }

    // 点击保存
    $('.btn-submit').click(function () {
      let value = input.value;
      console.log(value);

      if (!value) {
        $('.tip-content').html('请输入有效的数值')
        $('#tip-box').show();
        return;
      }

      let requestUrl = `${url}${api}`;

      $.ajax({
        type: 'GET',
        url: requestUrl,
        data: { value },
        success: function (res) {
          console.log(res);
          if (res.code == '0') {
            $('.tip-content').html('保存成功');
            $('#tip-box').show();
          } else {
            $('.tip-content').html(res.msg);
            $('#tip-box').show();
          }

          input.value = null;
        },
        error: function (xhr, status, error) {
          console.error('请求失败: ' + status + ' - ' + error);
          input.value = null;
        }
      })
    })

    $('.close-btn').click(function () {
      $('#tip-box').hide()
    })
    $('#tip-box').click(function () {
      $('#tip-box').hide()
    })
  </script>
</body>

</html>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1345661.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

SpringMVC源码解析——DispatcherServlet初始化

在Spring中&#xff0c;ContextLoaderListener只是辅助功能&#xff0c;用于创建WebApplicationContext类型的实例&#xff0c;而真正的逻辑实现其实是在DispatcherServlet中进行的&#xff0c;DispatcherServlet是实现Servlet接口的实现类。Servlet是一个JAVA编写的程序&#…

机器学习(二) -- 数据预处理(3)

系列文章目录 机器学习&#xff08;一&#xff09; -- 概述 机器学习&#xff08;二&#xff09; -- 数据预处理&#xff08;1-3&#xff09; 未完待续…… 目录 前言 tips&#xff1a;这里只是总结&#xff0c;不是教程哈。本章开始会用到numpy&#xff0c;pandas以及matpl…

DFS BFS

用DFS和BFS分别实现 //这边给出DFS的模版 void dfs(int x,int y) {//判断是否到达终点&#xff08;只有给出结束点的时候需要&#xff09; if (x ex && y ey) {if (min_steps > step) {min_steps step;}return;}//给出移动方向int move[4][2] {{0, 1}, {0, -1}…

如何使用python脚本生成redis格式的数据包

用python脚本生成redis格式的数据包 &#xff08;1&#xff09;使用下述网站下载开源的生成gopher协议规则的包的工具 https://github.com/firebroo/sec_tools/tree/master/redis-over-gopher &#xff08;2&#xff09;首先要修改redis.cmd中的内容 flushall config set di…

Linux 运维工具之1Panel

一、1Panel 简介 1Panel 是一个现代化、开源的 Linux 服务器运维管理面板。 特点&#xff1a; 快速建站&#xff1a;深度集成 Wordpress 和 Halo&#xff0c;域名绑定、SSL 证书配置等一键搞定&#xff1b;高效管理&#xff1a;通过 Web 端轻松管理 Linux 服务器&#xff0…

第一讲:BeanFactory和ApplicationContext

BeanFactory和ApplicationContext 什么是BeanFactory 它是ApplicationContext的父接口它才是Spring的核心容器&#xff0c;主要的ApplicationContext实现都组合了它的功能 BeanFactory能做什么? 表面上看BeanFactory的主要方法只有getBean()&#xff0c;实际上控制反转、基…

力扣:63. 不同路径 II(动态规划)

题目&#xff1a; 一个机器人位于一个 m x n 网格的左上角 &#xff08;起始点在下图中标记为 “Start” &#xff09;。 机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角&#xff08;在下图中标记为 “Finish”&#xff09;。 现在考虑网格中有障碍物。那…

【Matlab】基于遗传算法优化BP神经网络 (GA-BP)的数据时序预测

资源下载&#xff1a; https://download.csdn.net/download/vvoennvv/88682033 一&#xff0c;概述 基于遗传算法优化BP神经网络 (GA-BP) 的数据时序预测是一种常用的机器学习方法&#xff0c;用于预测时间序列数据的趋势和未来值。 在使用这种方法之前&#xff0c;需要将时间序…

visual studio + intel Fortran 错误解决

版本&#xff1a;VS2022 intel Fortran 2024.0.2 Package ID: w_oneAPI_2024.0.2.49896 共遇到三个问题。 1.rc.exe not found 2.kernel32.lib 无法打开 3.winres.h 无法打开 我安装时参考的教程&#xff1a;visual studio和intel oneAPI安装与编写fortran程序_visual st…

小巧的Windows Memory Cleaner内存清理工具-释放内存,提升电脑的性能-供大家学习研究参考

软件介绍 Windows Memory Cleaner是一款非常不错的内存清理工具大小仅200KB&#xff0c;这款免费的 RAM 清理器使用本机 Windows 功能来清理内存区域&#xff0c;帮助用户释放内存&#xff0c;提升电脑的性能&#xff0c;有时程序不会释放分配的内存&#xff0c;从而使计算机变…

【Vue2+3入门到实战】(15)VUE路由入门声明式导航的基本使用与详细代码示例

目录 一、声明式导航-导航链接1.需求2.解决方案3.通过router-link自带的两个样式进行高亮4.总结 二、声明式导航-两个类名1.router-link-active2.router-link-exact-active3.在地址栏中输入二级路由查看类名的添加4.总结 三、声明式导航-自定义类名&#xff08;了解&#xff09…

日志高亮 | notepad

高亮显示日志 日志文件无法清晰看到关键问题所在? 看到一堆日志头疼?高亮日志可以清晰展示出日志的 ERROR级等各种等级的问题, 一下浏览出日志关键所在 tailspin 项目地址&#xff1a; https://githubfast.com/bensadeh/tailspin 使用Rust包管理器cargo安装 安装 - Cargo 手…

LeetCode二叉树路径和专题:最大路径和与路径总和计数的策略

目录 437. 路径总和 III 深度优先遍历 前缀和优化 124. 二叉树中的最大路径和 437. 路径总和 III 给定一个二叉树的根节点 root &#xff0c;和一个整数 targetSum &#xff0c;求该二叉树里节点值之和等于 targetSum 的 路径 的数目。 路径 不需要从根节点开始&#xf…

【MYSQL】-函数

&#x1f496;作者&#xff1a;小树苗渴望变成参天大树&#x1f388; &#x1f389;作者宣言&#xff1a;认真写好每一篇博客&#x1f4a4; &#x1f38a;作者gitee:gitee✨ &#x1f49e;作者专栏&#xff1a;C语言,数据结构初阶,Linux,C 动态规划算法&#x1f384; 如 果 你 …

在线智能防雷监控检测系统应用方案

在线智能防雷监控检测系统是一种利用现代信息技术&#xff0c;对防雷设施的运行状态进行实时监测、管理和控制的系统&#xff0c;它可以有效提高防雷保护的安全性、可靠性和智能化程度&#xff0c;降低运维成本和风险&#xff0c;为用户提供全方位的防雷解决方案。 地凯科技在…

Vue常见面试问答

vue响应式数据 vue2 Vue2 的对象数据是通过 Object.defineProperty 对每个属性进行监听&#xff0c;当对属性进行读取的时候&#xff0c;就会触发 getter&#xff0c;对属性进行设置的时候&#xff0c;就会触发 setter。 /** * 这里的函数 defineReactive 用来对 Object.def…

2023.12.28 关于 Redis 数据类型 List 内部编码、应用场景

目录 List 编码方式 早期版本 现今版本 List 实际应用 多表之间的关联关系 消息队列 频道&#xff08;多列表&#xff09;消息队列 微博 Timeline 栈 & 队列 List 编码方式 早期版本 早期版本 List 类型的内部编码方式有两种 ziplist&#xff08;压缩列表&#xf…

论文阅读<Contrastive Learning-based Robust Object Detection under Smoky Conditions>

论文链接&#xff1a;https://openaccess.thecvf.com/content/CVPR2022W/UG2/papers/Wu_Contrastive_Learning-Based_Robust_Object_Detection_Under_Smoky_Conditions_CVPRW_2022_paper.pdf Abstract 目标检测是指有效地找出图像中感兴趣的目标&#xff0c;然后准确地确定它们…

ssrf之gopher协议的使用和配置,以及需要注意的细节

gopher协议 目录 gopher协议 &#xff08;1&#xff09;安装一个cn &#xff08;2&#xff09;使用Gopher协议发送一个请求&#xff0c;环境为&#xff1a;nc起一个监听&#xff0c;curl发送gopher请求 &#xff08;3&#xff09;使用curl发送http请求&#xff0c;命令为 …