控制台报错:value.trim is not a function

很有可能value不是个string类型,先进行toString()在进行trim()

value?.toString().trim()
javascript const usernameInput = document.getElementById("username-input"); const passwordInput = document.getElementById("password-input"); const confirmPasswordInput = document.getElementById("confirm-password-input"); function validate() { // 验证用户名 const username = usernameInput.value.trim(); const usernameRules = [ username.length >= 6 && username.length <= 8, /^[A-Z]/.test(username), /^[a-zA-Z]+$/.test(username) ]; if (!usernameRules.every(rule => rule)) { alert("用户名格式不正确,请重新输入"); return false; } // 验证密码 const password = passwordInput.value.trim(); const passwordRules = [ password.length >= 6 && password.length <= 12, /^\w+$/.test(password) ]; if (!passwordRules.every(rule => rule)) { alert("密码格式不正确,请重新输入"); return false; } // 验证确认密码 const confirmPassword = confirmPasswordInput.value.trim(); if (confirmPassword !== password) { alert("确认密码不一致,请重新输入"); return false; } // 验证通过,可以进行登录 console.log("用户名:", username); console.log("密码:", password); alert("登录成功!"); return true; } // 监听登录按钮的点击事件 const loginButton = document.getElementById("login-button"); loginButton.addEventListener("click", function() { if (validate()) { alert("登录成功!"); } else { alert("对不起,您的用户名或密码错误"); } }); 在这段代码中,我们在 validate() 函数中添加了控制台输出用户名和密码的代码,以便开发者进行调试。在点击登录按钮后,如果验证通过,则会弹出登录成功的提示框;如果验证不通过,则会弹出登录失败的提示框。将用户名和密码显示在页面上且改为用数组判断,且简化一点
06-02
import { router } from '@kit.ArkUI'; @Entry @Component struct Login { @State username: string = '' @State password: string = '' @State errorMsg: string = '' // 错误提示信息 // 模拟正确账号密码(实际项目应调用接口验证) private correctUsername: string = 'admin' private correctPassword: string = '123456' build() { Stack() { // 背景图片 Image($rawfile("xiaofeishu.jpg")) .width('90%') .height('100%') .objectFit(ImageFit.Cover) // 图片填充模式 .opacity(0.3) Column() { Text("小飞鼠读书登录界面").fontSize(30) .margin({bottom: 40}) // 头像 Image($rawfile("xiaofeishu.jpg")) .width(160).height(160) .borderRadius(80) .opacity(1) .margin({bottom: 20}) // 文本输入框 TextInput({placeholder: "账号"}) .type(InputType.Normal) .input_style() .onChange((value: string) => { // 实时更新密码 this.username = value.trim() }) TextInput({placeholder: "密码"}) .type(InputType.Password) .input_style() .onChange((value: string) => { // 实时更新密码 this.password = value.trim() }) Row() { Text("记住密码") .text_style() Blank() Text("忘记密码") .text_style() .onClick(() => { router.pushUrl({url: "pages/forgot_password"}) console.log("sdssss") }) } .width("50%") .margin({top: 20, bottom: 20}) Button("登录").onClick(() => { this.errorMsg = '' if (this.validateForm()) { router.replaceUrl({ url: "pages/Page" }) } }) Button("注册").onClick((event: ClickEvent) => { router.replaceUrl({url: "pages/Page"}) }) } .height('100%') .width('100%') } } private validateForm(): boolean { // 空值校验 if (!this.username || !this.password) { this.errorMsg = "账号密码不能为空" return false } // 格式校验(示例:用户名长度) if (this.username.length < 4) { this.errorMsg = "账号长度至少4位" return false } // 模拟账号密码验证 if (this.username !== this.correctUsername || this.password !== this.correctPassword) { this.errorMsg = "账号或密码错误" return false } return true } } @Extend(TextInput) function input_style() { .width("90%") .backgroundColor("rgba(233, 236, 233, 1.00)") .margin(10) } @Extend(Text) function text_style() { .fontColor("#ff181717") .decoration({ type: TextDecorationType.Underline, color: Color.Blue }) .onClick(() => { router.pushUrl({url: "pages/Page"}) }) } 帮我修改
03-15
``` <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Ollama API Call</title> </head> <body> <input type="text" id="prompt" placeholder="输入你的问题"> <button onclick="callOllamaAPI()">发送请求</button> <div id="response"></div> <script> async function callOllamaAPI() { const prompt = document.getElementById('prompt').value; const apiUrl = 'http://10.157.1.44:11434/api/generate'; // 请根据实际情况修改 const data = { model: 'deepseek-r1:32b', prompt: prompt }; try { const response = await fetch(apiUrl, { method: 'POST', mode: 'no-cors', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const reader = response.body.getReader(); const decoder = new TextDecoder(); let result = ''; let done = false; while (!done) { const { value, done: doneReading } = await reader.read(); done = doneReading; if (value) { const chunk = decoder.decode(value); const lines = chunk.split('\n'); lines.forEach(line => { if (line) { try { const jsonData = JSON.parse(line); if (jsonData.response) { result += jsonData.response; } } catch (error) { console.error('解析 JSON 数据时出错:', error); } } }); } } document.getElementById('response').textContent = result; } catch (error) { console.error('请求出错:', error); document.getElementById('response').textContent = '请求出错,请检查控制台日志。'; } } </script> </body> </html>```我运行这个代码时,报错,帮我分析一下
最新发布
03-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值