javascript-如何为动态输入设置最大值?
作者:互联网
我有Vue形式与动态输入.
1号用户以表格形式登记股份总数
2号他开始通过向他的股东部分添加所需的动态输入来开始向股东发放此总额
第三,他提交表格.
我的任务是避免发行过多.
我想不出如何避免向股东发行股份的数量超过总数/
Ex:
totalSharesAmount = 100
shareholder[0] - shares_amount = 50
shareholder[1] - shares_amount = 20 //70 total
shareholder[2] - shares_amount = 30 //100 total. can't be more than 100
我的资料:
data() {
return {
totatSharesAmount: 100
shareholders: [{share_amount: '', share_price: ''}]
}
}
我的验证方法(已计算),在这里我需要帮助:
sharesToFounders() {
return {
required: true,
max_value: this.totalSharesAmount - this.shareholder['shares_amount'] //need help here
}
}
解决方法:
解决问题的一种方法:跟踪当前总数,并在输入时使用验证器方法允许用户更改或阻止更改.
标记:
<div id="app">
<div class="row"><span>Total:</span><input type="number" v-model="totalPoints"></div>
<div v-for="(item, index) in foundersPoints" class="row">
<input type="number" v-on:input="updateValue(index)" v-model="foundersPoints[index].points_amount"><span v-if="foundersPoints[index].alert" class="alert">{{foundersPoints[index].alert}} <button v-on:click="dismissAlert(index)">OK</button></span>
</div>
</div>
验证器方法:
updateValue(idx) {
let value;
if (this.tempSum > this.totalPoints) {
const overage = this.totalPoints - this.tempSum;
value = Number(this.foundersPoints[idx].points_amount) + overage;
} else {
value = Number(this.foundersPoints[idx].points_amount);
}
this.foundersPoints[idx].points_amount = value;
},
用于跟踪的当前总计(计算的属性):
computed: {
tempSum() {
const pointsAmounts = this.foundersPoints.map(item => Number(item.points_amount));
return pointsAmounts.reduce((acc, t) => acc + Number(t), 0);
}
},
观看此小提琴的实际操作:https://jsfiddle.net/ebbishop/vu508Lgc/
这不适用于用户减少允许的总积分的情况. (用户的合计= 60,然后将三个点的值分别设置为20.如果用户将合计值更改为40,那么加起来为60的输入将如何处理?)
标签:vue-js,vee-validate,javascript 来源: https://codeday.me/bug/20191211/2106153.html