form { display: flex; /* 将表单元素设置为弹性盒子 */ justify-content: center; /* 将表单元素中的内容水平居中对齐 */ align-items: center; /* 将表单元素中的内容垂直居中对齐 */ } input[type="text"] { width: 300px; /* 设置输入框的宽度 */ padding: 10px; /* 设置输入框的内边距 */ border: 1px solid #ccc; /* 设置输入框的边框 */ border-radius: 5px; /* 设置输入框的圆角 */ font-size: 16px; /* 设置输入框中的字体大小 */ } button[type="submit"] { padding: 10px 20px; /* 设置按钮的内边距 */ border: none; /* 去掉按钮的边框 */ border-radius: 5px; /* 设置按钮的圆角 */ background-color: #007bff; /* 设置按钮的背景色 */ color: #fff; /* 设置按钮的文本颜色 */ font-size: 16px; /* 设置按钮中的字体大小 */ cursor: pointer; /* 设置鼠标悬停在按钮上时的光标样式 */ }把他向右移动
时间: 2023-10-26 08:04:44 浏览: 106
您可以使用以下 CSS 代码将这个搜索框向右移动:
```css
form {
display: flex; /* 将表单元素设置为弹性盒子 */
justify-content: flex-end; /* 将表单元素中的内容右对齐 */
}
input[type="text"] {
margin-right: 10px; /* 将输入框向右移动 10 像素 */
}
button[type="submit"] {
margin-right: 10px; /* 将提交按钮向右移动 10 像素 */
}
```
请注意,这只是一个示例代码,具体的像素值需要根据您的具体情况进行调整。另外,如果您需要在网页中使用这个搜索框并进行定位,建议将元素的位置调整到合适的位置,避免页面排版混乱。
相关问题
form { display: flex; /* 将表单元素设置为弹性盒子 / justify-content: center; / 将表单元素中的内容水平居中对齐 / align-items: center; / 将表单元素中的内容垂直居中对齐 /}input[type="text"] { width: 300px; / 设置输入框的宽度 / padding: 10px; / 设置输入框的内边距 / border: 1px solid #ccc; / 设置输入框的边框 / border-radius: 5px; / 设置输入框的圆角 / font-size: 16px; / 设置输入框中的字体大小 /}button[type="submit"] { padding: 10px 20px; / 设置按钮的内边距 / border: none; / 去掉按钮的边框 / border-radius: 5px; / 设置按钮的圆角 / background-color: #007bff; / 设置按钮的背景色 / color: #fff; / 设置按钮的文本颜色 / font-size: 16px; / 设置按钮中的字体大小 / cursor: pointer; / 设置鼠标悬停在按钮上时的光标样式 */}把他缩小一点
可以使用缩放(scale)属性或者改变元素的宽度和高度来缩小这段代码,以下是一个示例,将整个表单缩小到原来的 80%:
```css
form {
display: flex;
justify-content: center;
align-items: center;
transform: scale(0.8); /* 缩小 20% */
}
input[type="text"] {
width: 240px; /* 缩小输入框的宽度 */
font-size: 13px; /* 缩小输入框中的字体大小 */
}
button[type="submit"] {
padding: 8px 16px;
font-size: 13px;
}
```
<template> <div id="app"> <!-- 导航栏 --> <nav class="main-nav"> <div class="nav-group left"> <router-link to="/" class="nav-item">首页</router-link> <router-link to="/playmusic" class="nav-item">播放页</router-link> </div> <div class="nav-group right"> <router-link to="/login" class="login-logout">登录</router-link> <router-link to="/logout" class="login-logout">登出</router-link> </div> </nav> <!-- 路由视图容器 --> <router-view></router-view> </div> </template> <script> import { defineComponent } from 'vue' export default defineComponent({ name: 'App', }) </script> <style> #app { font-family: Avenir, Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; } .main-nav { padding: 20px; background: #f8f9fa; margin-bottom: 2rem; display: flex; /* 启用flex布局 */ justify-content: space-between; /* 左右分组自动分开 */ align-items: center; /* 垂直居中 */ } .nav-group { display: flex; gap: 15px; /* 现代浏览器间距设置 */ } .left{ margin-right: auto; } .right { margin-left: auto; /* 右侧组推到最右边 */ } .nav-item { text-decoration: none; color: #2c3e50; font-weight: bold; } .login-logout { margin: 0 15px; color: #2c3e50; text-align: right; text-decoration: none; /* 新增 */ } .login-logout:hover { opacity: 0.8; } .router-link-exact-active { color: #42b983; border-bottom: 2px solid #42b983; } </style> 实现登录/登出的逻辑,渲染登录的页面(需要提供无账号的时候提供注册的选项),登录与后端api访问用户是否存在,然后整个前端都要可以获取到登录状态,(登录状态下才可以进去播放页,否组提示登录)
<think>好的,我现在需要帮助用户实现登录和登出的逻辑,并确保整个前端能够获取登录状态。用户还提到在没有账号时需要提供注册选项,登录状态下才能进入播放页,否则提示登录。首先,我需要分析用户提供的代码结构和需求,然后逐步解决每个部分的问题。
首先,用户已经有一个Vue组件的模板,包含导航栏和路由视图。导航栏中有登录和登出的链接,但目前这些链接只是路由到/login和/logout路径。用户需要实现的是这些链接的实际功能,包括与后端API的交互、状态管理以及路由守卫。
接下来,我需要考虑如何管理用户的登录状态。在Vue中,通常可以使用Vuex来管理全局状态,或者使用Pinia(Vue的官方状态管理库)。不过,用户当前的代码中没有引入状态管理库,所以可能需要先添加Vuex或Pinia。不过,如果用户不想使用状态管理库,也可以考虑使用组合式API中的reactive来创建一个全局状态,或者利用本地存储(localStorage)来持久化登录状态。
然后,用户需要实现登录和注册的页面。在没有账号时,登录页面应该提供注册选项。这可能意味着在登录页面有一个链接跳转到注册页面,或者在同一页面中切换登录和注册的表单。需要根据用户的具体需求来设计,但用户提到需要提供注册选项,所以可能需要一个单独的注册页面或组件。
接下来,用户需要与后端API进行通信,检查用户是否存在。这意味着在登录表单提交时,前端需要发送POST请求到后端的登录接口,验证用户的凭据。如果成功,后端会返回一个token或用户信息,前端需要将其存储起来(如在localStorage或Vuex中),并更新登录状态。同样,注册功能需要将用户输入的信息发送到后端的注册接口,创建新用户。
然后,整个前端需要能够获取登录状态。这涉及到在应用初始化时检查本地存储中是否有有效的token,并据此设置登录状态。此外,需要在路由配置中添加导航守卫,检查用户是否已登录,才能访问受保护的路由(如播放页)。如果未登录,则重定向到登录页面并提示需要登录。
现在,考虑到用户提供的代码中使用了Vue Router,所以需要配置路由规则,确保播放页有路由守卫保护。同时,登录和登出的链接需要根据当前登录状态动态显示。例如,登录后显示登出按钮,未登录时显示登录按钮。
另外,用户提到要渲染登录页面,并在无账号时提供注册选项。这可能需要创建两个组件:Login和Register,或者在一个组件中切换两种表单。需要修改路由配置,添加这些页面的路由。
在样式方面,用户之前已经处理了去除下划线的问题,现在需要关注功能实现。但可能需要调整导航栏中登录/登出按钮的显示逻辑,根据登录状态切换显示。
现在,需要具体步骤:
1. 创建Login和Register组件,处理表单提交和API调用。
2. 设置全局状态管理,存储用户登录状态和token。
3. 修改路由配置,添加路由守卫,保护需要登录的路由。
4. 实现登录和登出的功能,包括API调用、状态更新和本地存储。
5. 在导航栏中根据登录状态动态显示登录或登出链接。
6. 处理注册流程,包括表单验证和API调用。
7. 在播放页组件中添加检查,如果未登录则提示并重定向。
在实现过程中需要注意以下几点:
- 安全性:确保敏感信息如token存储在安全的地方,避免XSS攻击。
- 错误处理:处理API调用中的错误,如网络问题、无效凭证等,并给出用户提示。
- 表单验证:前端应对用户输入进行基本验证,如邮箱格式、密码长度等。
- 用户体验:在登录成功后跳转到原请求的页面,或默认跳转到首页或播放页。
- 状态持久化:使用localStorage或sessionStorage保持登录状态,即使页面刷新也不会丢失。
现在需要检查用户的现有代码,看看是否有需要调整的地方。例如,在App.vue中,登录和登出链接目前是静态的,应该根据登录状态动态显示。可以使用v-if或v-show指令,根据状态显示不同的链接。
此外,登出功能需要清除本地存储中的token和状态,并重定向到登录页面或首页。
最后,需要测试整个流程,确保各环节正常工作,包括登录、注册、状态更新、路由保护和登出功能。</think>以下是分步骤实现方案,采用 Vue3 Composition API + Vue Router 方案:
---
### 一、路由配置(需新建 router/index.js)
```javascript
import { createRouter, createWebHistory } from 'vue-router'
const routes = [
{
path: '/',
name: 'Home',
component: () => import('@/views/Home.vue')
},
{
path: '/playmusic',
name: 'PlayMusic',
component: () => import('@/views/PlayMusic.vue'),
meta: { requiresAuth: true } // 添加权限标记
},
{
path: '/login',
name: 'Login',
component: () => import('@/views/Login.vue')
},
{
path: '/register',
name: 'Register',
component: () => import('@/views/Register.vue')
}
]
const router = createRouter({
history: createWebHistory(),
routes
})
// 全局路由守卫
router.beforeEach((to, from, next) => {
const isAuthenticated = localStorage.getItem('token')
if (to.matched.some(record => record.meta.requiresAuth)) {
if (!isAuthenticated) {
next({ name: 'Login', query: { redirect: to.fullPath } })
} else {
next()
}
} else {
next()
}
})
export default router
```
---
### 二、登录状态管理(推荐使用 Pinia)
1. 安装 Pinia:`npm install pinia`
2. 创建 store/auth.js:
```javascript
import { defineStore } from 'pinia'
export const useAuthStore = defineStore('auth', {
state: () => ({
user: null,
token: localStorage.getItem('token') || null
}),
actions: {
async login(credentials) {
// 调用后端API示例
const response = await fetch('/api/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(credentials)
})
const data = await response.json()
if (response.ok) {
this.token = data.token
localStorage.setItem('token', data.token)
return true
}
throw new Error(data.message || '登录失败')
},
logout() {
this.token = null
localStorage.removeItem('token')
this.router.push('/login')
}
}
})
```
---
### 三、登录页面组件(views/Login.vue)
```vue
<template>
<div class="login-container">
<h2>用户登录</h2>
<form @submit.prevent="handleLogin">
<div class="form-group">
<label>邮箱</label>
<input v-model="form.email" type="email" required>
</div>
<div class="form-group">
<label>密码</label>
<input v-model="form.password" type="password" required>
</div>
<button type="submit">登录</button>
<div class="register-link">
没有账号?<router-link to="/register">立即注册</router-link>
</div>
</form>
</div>
</template>
<script setup>
import { ref } from 'vue'
import { useAuthStore } from '@/store/auth'
import { useRouter } from 'vue-router'
const authStore = useAuthStore()
const router = useRouter()
const form = ref({
email: '',
password: ''
})
const handleLogin = async () => {
try {
await authStore.login(form.value)
const redirect = router.currentRoute.value.query.redirect || '/'
router.push(redirect)
} catch (error) {
alert(error.message)
}
}
</script>
```
---
### 四、注册页面组件(views/Register.vue)
```vue
<template>
<div class="register-container">
<h2>用户注册</h2>
<form @submit.prevent="handleRegister">
<div class="form-group">
<label>邮箱</label>
<input v-model="form.email" type="email" required>
</div>
<div class="form-group">
<label>密码</label>
<input v-model="form.password" type="password" required>
</div>
<button type="submit">注册</button>
</form>
</div>
</template>
<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
const router = useRouter()
const form = ref({
email: '',
password: ''
})
const handleRegister = async () => {
// 调用注册API
const response = await fetch('/api/register', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(form.value)
})
if (response.ok) {
alert('注册成功,请登录')
router.push('/login')
} else {
const error = await response.json()
alert(error.message || '注册失败')
}
}
</script>
```
---
### 五、导航栏动态显示优化(App.vue)
```vue
<script setup>
import { useAuthStore } from '@/store/auth'
import { computed } from 'vue'
const authStore = useAuthStore()
const isLoggedIn = computed(() => !!authStore.token)
</script>
<template>
<div class="nav-group right">
<router-link
v-if="!isLoggedIn"
to="/login"
class="login-logout"
>
登录
</router-link>
<a
v-else
href="#"
class="login-logout"
@click.prevent="authStore.logout"
>
登出
</a>
</div>
</template>
```
---
### 六、播放页权限提示(views/PlayMusic.vue)
```vue
<template>
<div v-if="authStore.isLoggedIn">
<!-- 正常播放页内容 -->
</div>
<div v-else class="auth-alert">
<p>请先登录以访问此页面</p>
<router-link to="/login">前往登录</router-link>
</div>
</template>
<script setup>
import { useAuthStore } from '@/store/auth'
const authStore = useAuthStore()
</script>
```
---
### 关键实现说明:
1. **路由守卫**:通过全局前置守卫实现自动跳转拦截
2. **状态持久化**:使用 localStorage 存储 token
3. **API交互**:使用 fetch API 与后端通信(需替换为实际接口地址)
4. **响应式显示**:通过 computed 属性动态显示登录状态
5. **错误处理**:统一捕获并显示 API 错误信息
实际开发中需要根据后端接口文档调整:
- 请求地址
- 请求/响应格式
- 错误处理逻辑
- 添加 JWT 请求头拦截器
阅读全文
相关推荐






請你按照上面要求將這段代碼重新修改,左邊為菜單欄,右邊是表格登記<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>异常记录</title> <style> .main { display: flex; flex-direction: row; justify-content: space-between; align-items: center; float: right; } .info { display: flex; align-items: center; } .info span { margin-right: 10px; } </style> </head> <body> 首页 登记数据 查询数据 异常记录登记 <form method="post" action="/https/wenku.csdn.net/submit"> <label>确认线别:</label> <input type="text" name="line">
<label>确认日期:</label> <input type="date" name="date">
<label>异常机种:</label> <input type="text" name="model">
<label>异常现象:</label> <textarea name="issue"></textarea>
<label>生产日期:</label> <input type="date" name="prod_date">
<label>生产班别:</label> <input type="text" name="shift">
<label>生产线别:</label> <input type="text" name="prod_line">
<label>责任人:</label> <input type="text" name="responsible">
<input type="submit" value="提交"> </form> 欢迎访问
</body> </html>





<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>用户登录</title> <style> /* 基础样式 */ body { font-family: 'Arial', sans-serif; background-color: #f0f2f5; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; } .login-container { background: white; padding: 40px; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); width: 350px; } h2 { color: #1a73e8; text-align: center; margin-bottom: 30px; } .form-group { margin-bottom: 20px; } label { display: block; margin-bottom: 8px; color: #5f6368; } input[type="text"], input[type="password"] { width: 100%; padding: 12px; border: 1px solid #dadce0; border-radius: 4px; box-sizing: border-box; font-size: 16px; } input[type="submit"] { background-color: #1a73e8; color: white; padding: 12px 20px; border: none; border-radius: 4px; cursor: pointer; width: 100%; font-size: 16px; transition: background-color 0.3s; } input[type="submit"]:hover { background-color: #1557b0; } .links { text-align: center; margin-top: 20px; } .links a { color: #1a73e8; text-decoration: none; font-size: 14px; margin: 0 10px; } </style> </head> <body> 用户登录 <form action="/https/wenku.csdn.net/login" method="POST"> <label for="username">用户名:</label> <input type="text" id="username" name="username" required> <label for="password">密码:</label> <input type="password" id="password" name="password" required> <input type="submit" value="登录"> </form> 忘记密码? 注册账号 </body> </html>这是网页前端代码,可以不用后端,基于nginx在点击的时候返回一个404,因为我的目的是监听网页

<!DOCTYPE html> <html lang=“zh-CN”> <head> <meta charset=“UTF-8”> <title>用户登录</title> <style> /* 基础样式 */ body { font-family: ‘Arial’, sans-serif; background-color: #f0f2f5; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; } .login-container { background: white; padding: 40px; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); width: 350px; } h2 { color: #1a73e8; text-align: center; margin-bottom: 30px; } .form-group { margin-bottom: 20px; } label { display: block; margin-bottom: 8px; color: #5f6368; } input[type="text"], input[type="password"] { width: 100%; padding: 12px; border: 1px solid #dadce0; border-radius: 4px; box-sizing: border-box; font-size: 16px; } input[type="submit"] { background-color: #1a73e8; color: white; padding: 12px 20px; border: none; border-radius: 4px; cursor: pointer; width: 100%; font-size: 16px; transition: background-color 0.3s; } input[type="submit"]:hover { background-color: #1557b0; } .links { text-align: center; margin-top: 20px; } .links a { color: #1a73e8; text-decoration: none; font-size: 14px; margin: 0 10px; } </style> </head> <body> 用户登录 <form action=“/login” method=“POST”> <label for=“username”>用户名:</label> <input type=“text” id=“username” name=“username” required> <label for="password">密码:</label> <input type="password" id="password" name="password" required> <input type="submit" value="登录"> </form> 忘记密码? 注册账号 </body> </html>这是网页前端代码,可以不用后端,基于nginx在点击的时候返回一个404,因为我的目的是监听网页

<!DOCTYPE html> <html lang=“zh-CN”> <head> <meta charset=“UTF-8”> <title>用户登录</title> <style> /* 基础样式 */ body { font-family: ‘Arial’, sans-serif; background-color: #f0f2f5; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; } .login-container { background: white; padding: 40px; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); width: 350px; } h2 { color: #1a73e8; text-align: center; margin-bottom: 30px; } .form-group { margin-bottom: 20px; } label { display: block; margin-bottom: 8px; color: #5f6368; } input[type="text"], input[type="password"] { width: 100%; padding: 12px; border: 1px solid #dadce0; border-radius: 4px; box-sizing: border-box; font-size: 16px; } input[type="submit"] { background-color: #1a73e8; color: white; padding: 12px 20px; border: none; border-radius: 4px; cursor: pointer; width: 100%; font-size: 16px; transition: background-color 0.3s; } input[type="submit"]:hover { background-color: #1557b0; } .links { text-align: center; margin-top: 20px; } .links a { color: #1a73e8; text-decoration: none; font-size: 14px; margin: 0 10px; } </style> </head> <body> 用户登录 <form action=“/login” method=“POST”> <label for=“username”>用户名:</label> <input type=“text” id=“username” name=“username” required> <label for="password">密码:</label> <input type="password" id="password" name="password" required> <input type="submit" value="登录"> </form> 忘记密码? 注册账号 </body> </html>我是windows系统,这是网页前端代码,可以不用后端,基于nginx在点击的时候返回一个404,因为我的目的是监听网页,如果有需要更改路径或需要添加额外操作的话请标示出来

<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>入库模块</title> <style> body { background-color: #f8f9fa; font-family: Arial, sans-serif; } .container { max-width: 1000px; min-height: 600px; /* 可根据需求调整这个值来改变整体框的最小高度 */ } .header { margin: 20px 0; color: #343a40; font-weight: bold; } .form-group { margin-bottom: 1.5rem; /* 增加表单组之间的间距 */ } .list-group-item { display: grid; grid-template-columns: repeat(3, 1fr); justify-content: space-between; align-items: center; width: 100%; padding: 5px 10px; border: 1px solid #dee2e6; border-radius: 5px; box-sizing: border-box; } .list-group-item span { text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } .card { border: none; border-radius: 10px; box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1); padding: 20px; background-color: #ffffff; } .btn-primary { background-color: #007bff; border: none; width: 200px; /* 设置按钮宽度为 50 像素 */ } .btn-primary:hover { background-color: #0056b3; } footer { margin-top: 20px; padding: 10px 0; background-color: #343a40; color: white; border-radius: 0 0 15px 15px; } /* 为查询按钮添加左外边距 */ .btn-query { margin-left: 100px; } /* 新增的按钮容器样式 */ .button-container { text-align: center; } /* 新增的样式,设置入库编号文本框的尺寸 */ .small-input { width: 150px; /* 你可以根据需要调整宽度 */ } /* 新增样式,使入库编号输入框在同一行显示 */ .id-inputs { display: flex; flex-wrap: wrap; align-items: center; } .id-inputs label { margin-right: 5px; } .id-row { display: flex; flex-wrap: wrap; align-items: center; width: 100%; } </style> </head> <body> 金铜线入库模块 <form method="POST"> <label for="item">整半卷类型</label> <select class="form-control" id="category" name="category" required> <option value="" disabled selected>请选择入库类型</option> <option value="整卷">整卷</option> <option value="半卷">半卷</option> {% if category %} <option value="{{ category }}" selected>{{ category }}</option> {% endif %} </select> <label for="item">入库型号</label> <select class="form-control" id="type" name="item" required> <option value="" disabled selected>请选择物品编号</option> <option value="70694">70694 - AU</option> <option value="70704">70704 - AU</option> <option value="70774">70774 - AU</option> <option value="70794">70794 - CU</option> <option value="70804">70708 - CU</option> <option value="70784">7058779 - CU</option> </select> <label for="batchNumber">入库批号</label> <input type="text" class="form-control" id="batchNumber" name="batchNumber" required> <label for="ID1">编号#1</label> <input type="text" class="form-control small-input" id="ID1" name="ID1"> <label for="ID2">编号#2</label> <input type="text" class="form-control small-input" id="ID2" name="ID2"> <label for="ID3">编号#3</label> <input type="text" class="form-control small-input" id="ID3" name="ID3"> <label for="ID4">编号#4</label> <input type="text" class="form-control small-input" id="ID4" name="ID4"> <label for="ID5">编号#5</label> <input type="text" class="form-control small-input" id="ID5" name="ID5"> <label for="ID6">编号#6</label> <input type="text" class="form-control small-input" id="ID6" name="ID6"> <label for="ID7">编号#7</label> <input type="text" class="form-control small-input" id="ID7" name="ID7"> <label for="ID8">编号#8</label> <input type="text" class="form-control small-input" id="ID8" name="ID8"> <label for="ID9">编号#9</label> <input type="text" class="form-control small-input" id="ID9" name="ID9"> <label for="ID10">编号#10</label> <input type="text" class="form-control small-input" id="ID10" name="ID10"> <label for="ID11">编号#11</label> <input type="text" class="form-control small-input" id="ID11" name="ID11"> <label for="ID12">编号#12</label> <input type="text" class="form-control small-input" id="ID12" name="ID12"> <label for="quantity">入库数量</label> <input type="number" class="form-control" id="quantity" name="quantity" value="1" required> <label for="date">入库日期</label> <input type="datetime-local" class="form-control" id="date" name="date" required value="{{ date }}"> <label for="shift">入库班次</label> <select class="form-control" id="shift" name="shift" required> <option value="" disabled selected>请选择班次</option> <option value="A">A</option> <option value="B">B</option> <option value="C">C</option> <option value="D">D</option> {% if shift %} <option value="{{ shift }}" selected>{{ shift }}</option> {% endif %} </select> <label for="barge">员工工号</label> <input type="text" class="form-control small-input" id="barge" name="barge" required value="{{ username }}"> <button type="submit" class="btn btn-primary">入库</button> <button type="button" class="btn btn-primary btn-query" id="queryButton">查询</button> </form> 当前库存 入库类型 在库型号 在库数量 {% for data in inventory_data %} {{ data.category }} {{ data.item }} {{ data.quantity }} {% endfor %} 返回首页 <script> function submitForm(event) { event.preventDefault(); // 阻止表单默认提交行为 // 获取表单数据 const data = { category: document.getElementById('category').value, type: document.getElementById('type').value, batchNumber: document.getElementById('batchNumber').value, ID1: document.getElementById('ID1').value, ID2: document.getElementById('ID2').value, ID3: document.getElementById('ID3').value, ID4: document.getElementById('ID4').value, ID5: document.getElementById('ID5').value, ID6: document.getElementById('ID6').value, ID7: document.getElementById('ID7').value, ID8: document.getElementById('ID8').value, ID9: document.getElementById('ID9').value, ID10: document.getElementById('ID10').value, ID11: document.getElementById('ID11').value, ID12: document.getElementById('ID12').value, quantity: document.getElementById('quantity').value, date: document.getElementById('date').value, shift: document.getElementById('shift').value, barge: document.getElementById('barge').value }; console.log(data) fetch('/into_storage', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }) .then(response => response.json()) .then(result => { alert(result.message); // 简单提示结果 if (result.status === 'success') { // 重置表单(可选) document.getElementById('batchNumber').value = ''; for(let i=1; i<=12; i++) { document.getElementById(ID${i}).value = ''; } } }) .catch(error => alert('网络错误: ' + error)); } // 绑定表单提交事件 document.querySelector('form').addEventListener('submit', submitForm); </script> </body> </html> 帮忙优化一下修改,要求绑定入库按钮id来触发传输数据到后端flask框架,使用fetch交互,其它按钮先不用管


