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

相关文章

Vue3 的 shallowRef 和 shallowReactive:优化性能

大家对 Vue3 的 ref 和 reactive 都很熟悉,那么对 shallowRef 和 shallowReactive 是否了解呢? 在编程和数据结构中,“shallow”(浅层)通常指对数据结构的最外层进行操作,而不递归地处理其内部或嵌套的数据。这种处理方式关注的是数据结构的第一层属性或元素,而忽略更深层次的嵌套内容。 1. 浅层与深层的对比 1.1 浅层(Shallow) 定义

JS常用组件收集

收集了一些平时遇到的前端比较优秀的组件,方便以后开发的时候查找!!! 函数工具: Lodash 页面固定: stickUp、jQuery.Pin 轮播: unslider、swiper 开关: switch 复选框: icheck 气泡: grumble 隐藏元素: Headroom

这15个Vue指令,让你的项目开发爽到爆

1. V-Hotkey 仓库地址: github.com/Dafrok/v-ho… Demo: 戳这里 https://dafrok.github.io/v-hotkey 安装: npm install --save v-hotkey 这个指令可以给组件绑定一个或多个快捷键。你想要通过按下 Escape 键后隐藏某个组件,按住 Control 和回车键再显示它吗?小菜一碟: <template

【 html+css 绚丽Loading 】000046 三才归元阵

前言:哈喽,大家好,今天给大家分享html+css 绚丽Loading!并提供具体代码帮助大家深入理解,彻底掌握!创作不易,如果能帮助到大家或者给大家一些灵感和启发,欢迎收藏+关注哦 💕 目录 📚一、效果📚二、信息💡1.简介:💡2.外观描述:💡3.使用方式:💡4.战斗方式:💡5.提升:💡6.传说: 📚三、源代码,上代码,可以直接复制使用🎥效果🗂️目录✍️

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06

在JS中的设计模式的单例模式、策略模式、代理模式、原型模式浅讲

1. 单例模式(Singleton Pattern) 确保一个类只有一个实例,并提供一个全局访问点。 示例代码: class Singleton {constructor() {if (Singleton.instance) {return Singleton.instance;}Singleton.instance = this;this.data = [];}addData(value)

计算机毕业设计 大学志愿填报系统 Java+SpringBoot+Vue 前后端分离 文档报告 代码讲解 安装调试

🍊作者:计算机编程-吉哥 🍊简介:专业从事JavaWeb程序开发,微信小程序开发,定制化项目、 源码、代码讲解、文档撰写、ppt制作。做自己喜欢的事,生活就是快乐的。 🍊心愿:点赞 👍 收藏 ⭐评论 📝 🍅 文末获取源码联系 👇🏻 精彩专栏推荐订阅 👇🏻 不然下次找不到哟~Java毕业设计项目~热门选题推荐《1000套》 目录 1.技术选型 2.开发工具 3.功能

Vue3项目开发——新闻发布管理系统(六)

文章目录 八、首页设计开发1、页面设计2、登录访问拦截实现3、用户基本信息显示①封装用户基本信息获取接口②用户基本信息存储③用户基本信息调用④用户基本信息动态渲染 4、退出功能实现①注册点击事件②添加退出功能③数据清理 5、代码下载 八、首页设计开发 登录成功后,系统就进入了首页。接下来,也就进行首页的开发了。 1、页面设计 系统页面主要分为三部分,左侧为系统的菜单栏,右侧

Node.js学习记录(二)

目录 一、express 1、初识express 2、安装express 3、创建并启动web服务器 4、监听 GET&POST 请求、响应内容给客户端 5、获取URL中携带的查询参数 6、获取URL中动态参数 7、静态资源托管 二、工具nodemon 三、express路由 1、express中路由 2、路由的匹配 3、路由模块化 4、路由模块添加前缀 四、中间件

【VUE】跨域问题的概念,以及解决方法。

目录 1.跨域概念 2.解决方法 2.1 配置网络请求代理 2.2 使用@CrossOrigin 注解 2.3 通过配置文件实现跨域 2.4 添加 CorsWebFilter 来解决跨域问题 1.跨域概念 跨域问题是由于浏览器实施了同源策略,该策略要求请求的域名、协议和端口必须与提供资源的服务相同。如果不相同,则需要服务器显式地允许这种跨域请求。一般在springbo