如何用逗号比较jQuery中的两种货币 [英] how to compare two currency in jquery with comma
本文介绍了如何用逗号比较jQuery中的两种货币的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个输入,当我开始输入数字时,它会自动更改为货币,如下所示:
I have two inputs that when I start typing number, it automatically changes to currency, like this:
1,000
10,000
100,000
1,000,000
1,000
10,000
100,000
1,000,000
那么您如何比较这两个输入?
因为它是逗号,所以会产生一个比较性的问题.
so how do you compare these two inputs?
Because it is a comma, it creates a comparative problem.
function priceCompare() {
var price_meterVal;
var priceVal;
$("#price_meter").on("keyup",function () {
price_meterVal = $($("#price_meter")).val().replace(/,/g, '');
});
$("#price").on("keyup",function () {
priceVal = $($("#price")).val().replace(/,/g, '');
});
if (priceVal <= price_meterVal){
$("#priceError").html('قیمت کل ملک نمی تواند کمتر از قیمت متری باشد.');
contractStatus = false;
}else {
contractStatus = true;
}
}
推荐答案
以下是几种方法.我将您发布的示例放在数组中,以避免再有4个变量.
Here are a few ways to do it. I put the examples you posted in an array to avoid having 4 more variables.
const sampleInputs = [ '1,000', '10,000', '100,000', '1,000,000' ]
// + is a shortcut to convert to a number
// split at commas
const splitMethod = +sampleInputs[0].split(',').join('')
// match digits
const regexOne = +(sampleInputs[1].match(/\d/g) || []).join('')
// replace commas
const regexTwo = +sampleInputs[2].replace(/,/g, '')
// filter
const fi = +sampleInputs[3]
.split('')
.filter(n => n !== ',')
.join('')
console.log('splitMethod', splitMethod)
console.log('regexOne', regexOne)
console.log('regexTwo', regexTwo)
console.log('filter', fi)
这篇关于如何用逗号比较jQuery中的两种货币的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文