业务场景,需要对人次以及金额进行正则匹配,人数是数字,而金额要允许小数,需要用到三元表达式:
直接上代码
其中使用v-on:input 来对事件进行绑定
<el-table-column
align="center"
label="填报值"
v-if="isOnly"
min-width="200px"
>
<template slot-scope="scope">
<span v-if="isCheck">{{ scope.row.value }}</span>
<el-input
v-else
v-model="scope.row.value"
placeholder="输入填报值"
clearable
v-on:input="inputChange(scope.row)"
@blur="scope.row.value = $event.target.value"
>
</el-input>
</template>
</el-table-column>
//方法
inputChange(val) {
//根据特定的字符串来判断是否使用 只允许数字还是可以数字加小数
val.value =
val.name.search(/人次|机构数/) !== -1
? val.value.replace(/[^\d]/g, "") //只允许数字
: val.value.replace(/[^\d^\.]+/g, ""); //允许数字加小数
},