如何比较jquery中的两种货币与逗号

问题描述 投票:0回答:3

我有两个输入,当我开始输入数字时,它会自动更改为货币,如下所示:

1,000 10,000 100,000 1,000,000

那么你如何比较这两个输入? 因为它是一个逗号,它会产生一个比较问题。

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;
    }
}
javascript jquery compare currency
3个回答
1
投票

以下是一些方法。我将您发布的示例放在一个数组中,以避免再增加4个变量。

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)

0
投票

你可以参考下面的代码行

function comparecurrent(cur1, cur2) {
        if (parseInt(cur1.replace(/,/g, '')) > parseInt(cur2.replace(/,/g, ''))) {
            alert("currency 1");
        }
        else if (parseInt(cur1.replace(/,/g, '')) < parseInt(cur2.replace(/,/g, ''))) 
        {
            alert("currency 2");
        }
        else {
            alert('equal');
        }
    }

0
投票

let newInteger = parseInt(numberString.split(",").join(''));

我假设你希望它最后是一个数字来与其他数字进行比较。如果你想保持一个字符串let newString = numberString.split(",").join('');

© www.soinside.com 2019 - 2024. All rights reserved.