js总结:onClick=“return confirm()”实现确认以及取消表单的提交

本文为一篇博客转载示例,详细说明了如何正确地引用其他博客的内容。通过实际案例展示了正确的引用格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载于:https://www.cnblogs.com/iCheny/p/8798865.html

import { IBestNavBar } from "@ibestservices/ibest-ui-v2"; import { IBestField } from "@ibestservices/ibest-ui-v2"; import { IBestButton } from "@ibestservices/ibest-ui-v2"; import { AxiosResponse } from '@ohos/axios' import {ZcUser} from '../interface/pord' import {ZcUsername}from '../api/Index' import log from '@open/log' @Entry @ComponentV2 export struct Zcuser { router=this.getUIContext().getRouter() @Local value: string = '' @Local value1: string = '' @Local value2: string = '' @Local value3: string = '' @Local value4: string = '' @Local count: number = 60 @Local timer: number = 0 @Local username:string='' @Local password:string='' @Local moblie:string='' @Local vCode:string='' @Local list:ZcUser|undefined=undefined Zclogin=async(data:ZcUser)=>{ let res:AxiosResponse=await ZcUsername(data) this.list=res.data log.info(this.list) } @Builder sendCodeButton() { IBestButton({ text: this.count == 60 ? '发送验证码' : `重新发送(${this.count})`, type: 'primary', buttonSize: 'mini', onBtnClick: () => { this.sendCode() } }) } sendCode() { if (this.timer) { return } this.count = 60 this.timer = setInterval(() => { if (this.count > 0) { this.count-- } else { clearInterval(this.timer) this.timer = 0 this.count = 60 } }, 1000) } @Builder leftBuilder(){ Row({space: 14}){ Image($rawfile("img/back.png")) .width(16) } } @Builder rightBuilder() { Row({ space: 14 }) { Image($rawfile('img/search.png')) .width(16) } } build() { Column() { IBestNavBar({ onLeftClick:()=>{ this.router.back() }, title: "乐淘云购", titleFontSize:30, titleColor: "#fff", navBarBgColor: "#458DF6", leftBuilder: (): void => this.leftBuilder(), isShowRight: true, rightBuilder: (): void => this.rightBuilder() }) Column(){ IBestField({ leftIcon:$rawfile('img/登录.png'), value: this.value1!!, placeholder: "请输入用户名", type: "password" }) IBestField({ leftIcon:$rawfile('img/phone.png'), value: this.value2!!, placeholder: "请输入手机号", type: "password" }) IBestField({ leftIcon:$rawfile('img/密码.png'), value: this.value3!!, placeholder: "请输入密码", type: "password" }) IBestField({ leftIcon:$rawfile('img/密码.png'), value: this.value4!!, placeholder: "请确认密码", type: "password" }) } IBestField({ value: this.value!!, leftIcon:$rawfile('img/code.png'), placeholder: "请输入验证码", hasBorder: false, buttonBuilder: (): void => this.sendCodeButton() }) Button('立即注册') .width('100%') .margin({top:20}) .onClick(()=>{ }) Flex({justifyContent:FlexAlign.End}) { Text('立即登录') .onClick(() => { this.router.pushUrl({ url: "pages/Login" }) }) .fontColor("#0A59F7") .margin({ top: 30 }) } } .width('100%') .height('100%') .backgroundColor('#f5f5f5') } }帮我写注册功能 我提供接口export interface ZcUser{ username:string password:string mobile:string vCode:string }export const ZcUsername=(data:ZcUser)=>api.post('user/register',data)
07-30
import { IBestNavBar, IBestField, IBestButton } from "@ibestservices/ibest-ui-v2"; import { AxiosResponse } from '@ohos/axios' import { ZcUser } from '../interface/pord' import { ZcUsername } from '../api/Index' import log from '@open/log' import prompt from '@system.prompt' @Entry @ComponentV2 export struct Zcuser { router = this.getUIContext().getRouter() @Local count: number = 60 @Local timer: number = 0 @Local username: string = '' @Local password: string = '' @Local mobile: string = '' @Local confirmPassword: string = '' @Local vCode: string = '' @Local list: ZcUser | undefined = undefined // 注册方法 ZcRegister = async () => { if (!this.validateForm()) return; const regData: ZcUser = { username: this.username, password: this.password, mobile: this.mobile, vCode: this.vCode }; try { const res: AxiosResponse = await ZcUsername(regData); this.list = res.data; log.info('注册成功'); prompt.showToast({ message: '注册成功!', duration: 2000 }); setTimeout(() => { this.router.pushUrl({ url: "pages/Login" }); }, 2000); } catch (error) { log.error('注册失败', error); prompt.showToast({ message: '注册失败: ' + (error.message || '未知错误'), duration: 3000 }); } } // 表单验证 validateForm(): boolean { if (!this.username.trim()) { prompt.showToast({ message: '请输入用户名', duration: 2000 }); return false; } if (!this.mobile.trim()) { prompt.showToast({ message: '请输入手机号', duration: 2000 }); return false; } const mobileRegex = /^1[3-9]\d{9}$/; if (!mobileRegex.test(this.mobile)) { prompt.showToast({ message: '请输入正确的手机号', duration: 2000 }); return false; } if (!this.password) { prompt.showToast({ message: '请输入密码', duration: 2000 }); return false; } if (this.password.length < 6) { prompt.showToast({ message: '密码长度至少6位', duration: 2000 }); return false; } if (this.password !== this.confirmPassword) { prompt.showToast({ message: '两次输入的密码不一致', duration: 2000 }); return false; } if (!this.vCode) { prompt.showToast({ message: '请输入验证码', duration: 2000 }); return false; } return true; } @Builder sendCodeButton() { IBestButton({ text: this.count == 60 ? '发送验证码' : `重新发送(${this.count})`, type: 'primary', buttonSize: 'mini', onBtnClick: () => { this.sendCode() } }) } sendCode() { if (!this.mobile) { prompt.showToast({ message: '请输入手机号', duration: 2000 }); return; } const mobileRegex = /^1[3-9]\d{9}$/; if (!mobileRegex.test(this.mobile)) { prompt.showToast({ message: '手机号格式错误', duration: 2000 }); return; } if (this.timer) return; prompt.showToast({ message: '验证码已发送', duration: 2000 }); this.count = 60; this.timer = setInterval(() => { if (this.count > 0) { this.count--; } else { clearInterval(this.timer); this.timer = 0; this.count = 60; } }, 1000); } @Builder leftBuilder() { Row({ space: 14 }) { Image($rawfile("img/back.png")) .width(16) } } @Builder rightBuilder() { Row({ space: 14 }) { Image($rawfile('img/search.png')) .width(16) } } build() { Column() { IBestNavBar({ onLeftClick: () => this.router.back(), title: "乐淘云购", titleFontSize: 30, titleColor: "#fff", navBarBgColor: "#458DF6", leftBuilder: (): void => this.leftBuilder(), isShowRight: true, rightBuilder: (): void => this.rightBuilder() }) Column() { // 用户名输入框 - 直接使用值 IBestField({ leftIcon: $rawfile('img/登录.png'), value: this.username, placeholder: "请输入用户名", type: "text", onChange: (value: string) => { this.username = value; } }) // 手机号输入框 - 直接使用值 IBestField({ leftIcon: $rawfile('img/phone.png'), value: this.mobile, placeholder: "请输入手机号", type: "number", onChange: (value: string) => { this.mobile = value; } }) // 密码输入框 - 直接使用值 IBestField({ leftIcon: $rawfile('img/密码.png'), value: this.password, placeholder: "请输入密码", type: "password", onChange: (value: string) => { this.password = value; } }) // 确认密码输入框 - 直接使用值 IBestField({ leftIcon: $rawfile('img/密码.png'), value: this.confirmPassword, placeholder: "请确认密码", type: "password", onChange: (value: string) => { this.confirmPassword = value; } }) } // 验证码输入框 - 直接使用值 IBestField({ value: this.vCode, leftIcon: $rawfile('img/code.png'), placeholder: "请输入验证码", hasBorder: false, onChange: (value: string) => { this.vCode = value; }, buttonBuilder: (): void => this.sendCodeButton() }) // 注册按钮 Button('立即注册') .width('100%') .margin({ top: 20 }) .onClick(() => this.ZcRegister()) // 跳转登录 Flex({ justifyContent: FlexAlign.End }) { Text('立即登录') .onClick(() => this.router.pushUrl({ url: "pages/Login" })) .fontColor("#0A59F7") .margin({ top: 30 }) } } .width('100%') .height('100%') .backgroundColor('#f5f5f5') } }不用输入框组件
最新发布
07-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值