笔记-input type=‘number‘ 可以输入e问题

import React, { useState } from 'react';

function App() {
  const [value, setValue] = useState('');
  const handleInput = (e) => {
    const input = e.target.value;
   
      setValue(input);
   
  };
  return (
    <>
      <input type='number' value={value} onChange={handleInput} />
    </>
  );
}

export default App;

输入后发现还可以输入 “e”?(同样问题在Material UI 的TextField使用中,也会出现该问题)
在这里插入图片描述

input type=‘number’ 中为什么可以输入“e”呢?以下为百度搜索的资料
在HTML的input元素中,type属性为"number"的输入框通常用于接收数字输入。然而,即使设置了"type=number",输入框仍然允许输入科学计数法表示的数字,包括 “e” 或 “E”。
科学计数法是一种表示非常大或非常小的数字的方法,通过使用乘以10的幂来表示。例如,1亿可以表示为1e8,0.000001可以表示为1e-6。
因此,当用户在"type=number"的输入框中输入类似于"1e8"或"1e-6"的值时,浏览器会将其解释为相应的科学计数法表示的数字,并将其视为有效的输入。但需要注意的是,并非所有浏览器都支持科学计数法的输入,因此在实际使用中可能会有一些差异。

我想到的方法:在setValue之前加个正则判断,为纯数字才让它set进去
前言:
1、在if判断中需要加 input===“” , 因为可以让输入的数字清空

function App() {
  const [value, setValue] = useState('');
  const handleInput = (e) => {
    const reg = /^\d+$/;
    const input = e.target.value;
    if (reg.test(input) || input === '') {
      console.log('输入的值为:', input);
      setValue(input);
    }
  };
  return (
    <>
      <input type="number" value={value} onChange={handleInput} />
    </>
  );
}

我在此处连续输入:123e45 结果会存在帮我清空输入框的问题
在这里插入图片描述造成分析:
当我输入123e的时候,控制台输出:输入的值为空==>“”(什么原因我也不清楚),因为输入的值变成了“” ,所以进入到我们if判断中的input===“”,所以setValue成功
解决方法:换个思路把 input 标签中的 type="number"删除,把他当做一个普通的输入框去使用,再使用纯数字的正则去匹配value

import React, { useState } from 'react';

function App() {
  const [value, setValue] = useState('');
  const handleInput = (e) => {
    const reg = /^\d+$/;
    const input = e.target.value;
    if (reg.test(input) || input === '') {
      console.log('输入的值为:', input);
      setValue(input);
    }
  };
  return (
    <>
      <input value={value} onChange={handleInput} />
    </>
  );
}

export default App;

上图:我再次输入:123e456
在这里插入图片描述
问题已经解决!

<template> <el-table :data="tableData" style="width: 100%"> <el-table-column type="selection" width="55"/> <el-table-column label="产品名称" width="220"> <template #default="scope"> <div class="product-name-wrapper"> <el-link type="primary" :underline="false" @click="goToProductDetail(scope.row.id)"> <span >{{ scope.row.name }}</span> <span style="color: brown;" >※点击进入详细画面</span> </el-link> </div> </template> </el-table-column> <el-table-column property="categoryName" label="产品分类"width="120"/> <el-table-column property="price" label="产品单价"width="120"/> <el-table-column property="createAt" label="生产日期"width="250"/> <el-table-column property="price" label="单价"width="120"/> <el-table-column property="stock" label="库存"width="120"/> <el-table-column label="购买数量" width="180"> <template #default="scope"> <div class="quantity-control"> <el-input-number v-model="scope.row.number" :min="0" :max="scope.row.stock" size="small" controls-position="right" @change="handleQuantityChange(scope.$index,scope.row)" /> </div> </template> </el-table-column> <el-table-column label="合计" width="120"> <template #default="scope"> {{ formatCurrency(scope.row.price * scope.row.number) }} </template> </el-table-column> <el-table-column label="操作" width="180"> <template #default="scope"> <el-button size="small" @click="handleEdit(scope.$index,scope.row)"> 编辑 </el-button> <el-button size="small" type="danger" @click="handleDelete(scope.$index,scope.row)"> 删除 </el-button> </template> </el-table-column> </el-table> </template> <script setup> const router = useRouter(); const user = "aaa" import productApi from '@/api/products' import { ref } from 'vue'; const goToProductDetail = (productId) => { router.push(`/home/test${productId}`) } const handleEdit = (index, row) => { console.log(index, row) } const handleDelete = (index, row) => { console.log(index, row) } // 处理数量变化事件 const handleQuantityChange = (index, row) => { console.log(`产品 ${row.name} 数量更新为: ${row.number}`); // 这里可以添加更新合计金额的逻辑 } const formatCurrency = (value) => { const numValue = parseFloat(value); // 先转换为数字 if (isNaN(numValue)) { return '¥0.00'; } return `¥${Math.abs(numValue).toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,')}`; }; //商品一览 function getProduct() { productApi.getAll(user).then(res => { tableData.value = res.data; }) } onMounted(() => { getProduct(); }) const tableData = ref(); </script> <style scoped> .aaa-tag { display: inline-block; background-color: #409eff; color: white; font-size: 12px; padding: 0 4px; border-radius: 3px; margin-right: 5px; } </style> 购买数量默认是0
最新发布
07-11
<template> <el-table :data="tableData" style="width: 100%"> <el-table-column type="selection" width="55"/> <el-table-column label="产品名称" width="220"> <template #default="scope"> <div class="product-name-wrapper"> <el-link type="primary" :underline="false" @click="goToProductDetail(scope.row.id)"> <span >{{ scope.row.name }}</span> <span style="color: brown;" >※点击进入详细画面</span> </el-link> </div> </template> </el-table-column> <el-table-column property="categoryName" label="产品分类"width="120"/> <el-table-column property="price" label="产品单价"width="120"/> <el-table-column property="createAt" label="生产日期"width="250"/> <el-table-column property="price" label="单价"width="120"/> <el-table-column property="stock" label="库存"width="120"/> <el-table-column label="购买数量" width="180"> <template #default="scope"> <div class="quantity-control"> <el-input-number v-model="scope.row.number" :min="1" size="small" controls-position="right" @change="handleQuantityChange(scope.$index,scope.row)" /> </div> </template> </el-table-column> <el-table-column label="合计" width="120"> <template #default="scope"> {{ formatCurrency(scope.row.price * scope.row.number) }} </template> </el-table-column> <el-table-column label="操作" width="180"> <template #default="scope"> <el-button size="small" @click="handleEdit(scope.$index,scope.row)"> 编辑 </el-button> <el-button size="small" type="danger" @click="handleDelete(scope.$index,scope.row)"> 删除 </el-button> </template> </el-table-column> </el-table> </template> <script setup> const router = useRouter(); const user = "aaa" import productApi from '@/api/products' import { ref } from 'vue'; const goToProductDetail = (productId) => { router.push(`/home/test${productId}`) } const handleEdit = (index, row) => { console.log(index, row) } const handleDelete = (index, row) => { console.log(index, row) } // 处理数量变化事件 const handleQuantityChange = (index, row) => { console.log(`产品 ${row.name} 数量更新为: ${row.number}`); // 这里可以添加更新合计金额的逻辑 } // 格式化货币显示 const formatCurrency = (value) => { return '¥' + value.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,'); }; //商品一览 function getProduct() { productApi.getAll(user).then(res => { tableData.value = res.data; }) } onMounted(() => { getProduct(); }) const tableData = ref(); </script> <style scoped> .aaa-tag { display: inline-block; background-color: #409eff; color: white; font-size: 12px; padding: 0 4px; border-radius: 3px; margin-right: 5px; } </style> 计算不好使
07-11
<template> <el-table :data="tableData" style="width: 100%"> <el-table-column type="selection" width="55"/> <el-table-column label="产品名称" width="220"> <template #default="scope"> <div class="product-name-wrapper"> <el-link type="primary" :underline="false" @click="goToProductDetail(scope.row.id)"> <span >{{ scope.row.name }}</span> <span style="color: brown;" >※点击进入详细画面</span> </el-link> </div> </template> </el-table-column> <el-table-column property="date" label="产品分类"width="120"/> <el-table-column property="date" label="产品单价"width="120"/> <el-table-column property="date" label="产品名称"width="120"/> <el-table-column property="date" label="产品分类"width="120"/> <el-table-column property="date" label="产品单价"width="120"/> <el-table-column property="date" label="生产日期"width="120"/> <el-table-column property="date" label="单价"width="120"/> <el-table-column property="date" label="库存"width="120"/> <el-table-column property="date" label="购买数量"width="120"/> <el-table-column property="date" label="合计"width="120"/> <el-table-column label="操作" width="180"> <template #default="scope"> <el-button size="small" @click="handleEdit(scope.$index,scope.row)"> 编辑 </el-button> <el-button size="small" type="danger" @click="handleDelete(scope.$index,scope.row)"> 删除 </el-button> </template> </el-table-column> </el-table> </template> <script setup> const router = useRouter(); const goToProductDetail = (productId) => { router.push(`/home/test${productId}`) } const handleEdit = (index, row) => { console.log(index, row) } const handleDelete = (index, row) => { console.log(index, row) } const tableData = [ { date: '2016-05-04', name: 'Aleyna', address: 'Lohrbergstr. 86c, Süd Lilli, Saarland', }, { date: '2016-05-03', name: 'Helen', address: '760 A Street, South Frankfield, Illinois', }, { date: '2016-05-02', name: 'Brandon', address: 'Arnold-Ohletz-Str. 41a, Alt Malinascheid, Thüringen', }, { date: '2016-05-01', name: 'Margie', address: '23618 Windsor Drive, West Ricardoview, Idaho', } ] </script> <style scoped> .aaa-tag { display: inline-block; background-color: #409eff; color: white; font-size: 12px; padding: 0 4px; border-radius: 3px; margin-right: 5px; } </style>   我想在购买数量中加上数字输入框左边加号右边减号
07-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大白砌墙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值