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

2023-12-31 04:44

本文主要是介绍html文件Js写输入框和弹框调接口jQuery,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 业务场景:需要使用写一个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>

这篇关于html文件Js写输入框和弹框调接口jQuery的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/554971

相关文章

Vue和React受控组件的区别小结

《Vue和React受控组件的区别小结》本文主要介绍了Vue和React受控组件的区别小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录背景React 的实现vue3 的实现写法一:直接修改事件参数写法二:通过ref引用 DOMVu

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1

SpringBoot实现不同接口指定上传文件大小的具体步骤

《SpringBoot实现不同接口指定上传文件大小的具体步骤》:本文主要介绍在SpringBoot中通过自定义注解、AOP拦截和配置文件实现不同接口上传文件大小限制的方法,强调需设置全局阈值远大于... 目录一  springboot实现不同接口指定文件大小1.1 思路说明1.2 工程启动说明二 具体实施2

Vue3绑定props默认值问题

《Vue3绑定props默认值问题》使用Vue3的defineProps配合TypeScript的interface定义props类型,并通过withDefaults设置默认值,使组件能安全访问传入的... 目录前言步骤步骤1:使用 defineProps 定义 Props步骤2:设置默认值总结前言使用T

Three.js构建一个 3D 商品展示空间完整实战项目

《Three.js构建一个3D商品展示空间完整实战项目》Three.js是一个强大的JavaScript库,专用于在Web浏览器中创建3D图形,:本文主要介绍Three.js构建一个3D商品展... 目录引言项目核心技术1. 项目架构与资源组织2. 多模型切换、交互热点绑定3. 移动端适配与帧率优化4. 可

基于Redisson实现分布式系统下的接口限流

《基于Redisson实现分布式系统下的接口限流》在高并发场景下,接口限流是保障系统稳定性的重要手段,本文将介绍利用Redisson结合Redis实现分布式环境下的接口限流,具有一定的参考价值,感兴趣... 目录分布式限流的核心挑战基于 Redisson 的分布式限流设计思路实现步骤引入依赖定义限流注解实现

基于Python Playwright进行前端性能测试的脚本实现

《基于PythonPlaywright进行前端性能测试的脚本实现》在当今Web应用开发中,性能优化是提升用户体验的关键因素之一,本文将介绍如何使用Playwright构建一个自动化性能测试工具,希望... 目录引言工具概述整体架构核心实现解析1. 浏览器初始化2. 性能数据收集3. 资源分析4. 关键性能指

SpringBoot实现RSA+AES自动接口解密的实战指南

《SpringBoot实现RSA+AES自动接口解密的实战指南》在当今数据泄露频发的网络环境中,接口安全已成为开发者不可忽视的核心议题,RSA+AES混合加密方案因其安全性高、性能优越而被广泛采用,本... 目录一、项目依赖与环境准备1.1 Maven依赖配置1.2 密钥生成与配置二、加密工具类实现2.1

使用Python的requests库调用API接口的详细步骤

《使用Python的requests库调用API接口的详细步骤》使用Python的requests库调用API接口是开发中最常用的方式之一,它简化了HTTP请求的处理流程,以下是详细步骤和实战示例,涵... 目录一、准备工作:安装 requests 库二、基本调用流程(以 RESTful API 为例)1.