Giriş kutusuna eklenen bir sayının giriş değerini yeniden kodlamak için bir JavaScript/JQuery yöntemi yazıyorum. Çekimler olmayan koleksiyon, ondalık ayırıcının önündeki sayılardır. Bu komut dosyasını çalıştırmak Ancak, ben şu çizgide bu sorunu olsun:JavaScript: Koleksiyon döngü için boştur
for(var i=0 ; i < nonFractions.length ; i++) {
Benim değişken nonFractions eşit olduğu null
için. Bu nasıl oluyor? Meseleyi göremiyorum.
Benim toplam kodu:
$("#queryInstructedAmountFrom").change(function(){
var amount = $("#queryInstructedAmountFrom").val();
amount = removeZeros(amount);
var splittedValues = amount.split(/[,.]/);
amount = "";
if(splittedValues.length == 1) {
amount += splittedValues[0];
var nonFractions = amount.match(/.{1,3}/g);
var firstIndex = amount.length % 3;
if(firstIndex != 0) {
var firstNumbers = amount.substr(0, firstIndex);
amount = amount.substr(firstIndex);
nonFractions = amount.match(/.{1,3}/g);
amount = "";
amount += firstNumbers;
amount += thousandSeparator;
} else {
amount = "";
}
for(var i=0 ; i < nonFractions.length ; i++) {
amount += nonFractions[i];
if(i < (nonFractions.length - 1)){
amount += thousandSeparator;
}
}
amount += decimalSeparator;
amount += "00";
} else {
for(var i=0 ; i < splittedValues.length - 1 ; i++) {
amount += splittedValues[i];
}
nonFractions = amount.match(/.{1,3}/g);
var firstIndex = amount.length % 3;
alert(firstIndex);
if(firstIndex == 0) {
nonFractions = amount.match(/.{1,3}/g);
}
if(firstIndex > 1) {
var firstNumbers = amount.substr(0, firstIndex);
amount = amount.substr(firstIndex);
nonFractions = amount.match(/.{1,3}/g);
amount = "";
amount += firstNumbers;
amount += thousandSeparator;
} else {
alert(nonFractions);
amount = "";
}
for(var i=0 ; i < nonFractions.length ; i++) {
amount += nonFractions[i];
if(i < (nonFractions.length - 1)){
amount += thousandSeparator;
}
}
amount += decimalSeparator;
amount += splittedValues[splittedValues.length -1];
}
Biri bana çözümü verebilir misiniz? Aradığınız regex bulamazsa amount.match
Eğer
Bir sorunla karşılaşırdınız, çünkü değişken bildiriminiz sizin için başlıyorsa, deyiminiz var mı? SplittedValues.length! = 1 ise ne olur? – Dresden