vue 輸入框輸入任意內(nèi)容返回數(shù)字的實現(xiàn)
更新時間:2022年03月30日 15:26:55 作者:前端醬紫
本文主要介紹了vue 輸入框輸入任意內(nèi)容返回數(shù)字的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
本文主要介紹了vue 輸入框輸入任意內(nèi)容返回數(shù)字,具體如下:
輸入任意內(nèi)容只返回數(shù)字
// 提取數(shù)字 傳入數(shù)字 export function changeEvent(item) { let nums = item + ""; if (nums === "") { nums = ""; // 空的話 直接返回空 } else { nums = nums.replace(/[^\d|\.]/g, ""); // 提取出來 一定是數(shù)字 if (nums.includes(".")) { // 包含小數(shù)點 let strL = nums.substring(0, nums.indexOf(".")); let strR = nums.substring(nums.indexOf(".") + 1); nums = strL + "." + strR; } // else { // // 不包含小數(shù)點 // nums = nums.replace(/[^\d|\.]/g, ""); // 提取出來 一定是數(shù)字 // } } return nums; } // 離開輸入事件 export function blurEvent(x, y) { if (x === "") { y.manualScore = ""; } else { x = x + ""; let nums; if (x.includes(".")) { // 包含小數(shù)點 nums = x.replace(/[^\d|\.]/g, ""); // 提取出來 一定是數(shù)字 let strL = nums.substring(0, nums.indexOf(".")); let strR = nums.substring(nums.indexOf(".") + 1); strR = strR.replace(/\./gi, ""); // 去除多余小數(shù)點 if (strL === "" && strR === "") { nums = "0"; // 有小數(shù)點,但左側(cè)右側(cè)都為空 默認(rèn)為0 } else if (strL === "" && strR !== "") { // 左側(cè)為空 右側(cè)不為空 小數(shù) nums = "0." + strR; } else if (strL !== "" && strR === "") { // 右側(cè)為空 左側(cè)不為空 整數(shù) nums = strL; } else if (strL !== "" && strR !== "") { nums = strL + "." + strR; } } else { nums = x.replace(/[^\d|\.]/g, ""); // 提取出來 一定是數(shù)字 } let z = nums * 1 || ""; z = z < 0 ? 0 : z; y.manualScore = z; } console.log("x", x, "y:", y); }
到此這篇關(guān)于vue 輸入框輸入任意內(nèi)容返回數(shù)字的實現(xiàn)的文章就介紹到這了,更多相關(guān)vue 輸入框輸入返回數(shù)字內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談vue項目優(yōu)化之頁面的按需加載(vue+webpack)
本篇文章主要介紹了vue項目優(yōu)化之頁面的按需加載(vue+webpack),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12在 Vue3 中如何使用 styled-components
styled-components 的官方 Vue 版本目前已多年沒有更新,而且只支持到 Vue2,那么,在 Vue3 中怎么才能使用到 styled-components 呢,下面給大家介紹在 Vue3 中使用 styled-components的相關(guān)知識,感興趣的朋友跟隨小編一起看看吧2024-05-05在vue.js渲染完界面之后如何再調(diào)用函數(shù)
這篇文章主要介紹了在vue.js渲染完界面之后如何再調(diào)用函數(shù)的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07vue-axios同時請求多個接口 等所有接口全部加載完成再處理操作
這篇文章主要介紹了vue-axios同時請求多個接口 等所有接口全部加載完成再處理操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11