我有一個函數可以從輸入字段中獲取數字,數字在逗號而不是小數點上的點(123,5),所以我所做的就是將它們與替換進行轉換以使計算,然後再更換他們逗號來顯示結果,但它給我的問題,在過去的更換,這是一個Ajax回報Ajax將數字點替換爲逗號不起作用
錯誤:"ncaught TypeError: subt1.replace is not a function at XMLHttpRequest.ajax.onreadystatechange"
我得到的計算正確的,但是當我嘗試將其轉換爲逗號小數不起作用 我起因是因爲打字,但我沒弄錯它
繼承人我的代碼
function updateCarrito(id,precio,moneda) {
var cant=document.getElementsByName("carrito_cantidad_"+id)[0].value;
var ajax=nuevoAjax();
var contenedor=parent.document.getElementById('iconCarrito');
var subt1=document.getElementById('cantidad_compra1_'+id).value;
if (moneda==1){
var tot=document.getElementById('total_compra_pesos').value;
}else{
var tot=document.getElementById('total_compra_dolares').value;
}
subt1 = subt1.replace(/\./g, '').replace(',', '.');
tot = tot.replace(/\./g, '').replace(',', '.');
ajax.open("GET", "http://www.somestuff.com?req=updateCarrito&id="+id+"&cant="+cant+"&precio="+precio,true);
ajax.onreadystatechange=function() {
if (ajax.readyState==4) {
contenedor.innerHTML = ajax.responseText;
tot=(tot-subt1);
subt1=(precio*cant);
subt1 = subt1.replace('.', ',');
document.getElementById('cantidad_compra1_'+id).value=subt1;
tot=(tot+subt1);
tot = tot.replace('.', ',');
if (moneda==1){
document.getElementById('total_compra_pesos').value=tot;
document.getElementById('total_compra_pesos2').value=tot;
}else{
document.getElementById('total_compra_dolares').value=tot;
document.getElementById('total_compra_dolares2').value=tot;
}
}
};
ajax.send(null);
}
你改寫'subt1'的'值的任何一個後如預期器SUBT1 =(PRECIO *斜面);'。我認爲它不再是一個字符串,現在它是一個數字。 '.replace'方法僅適用於字符串。 – surajck
是的,你是對的,但我無法弄清楚如何轉換它來改變,我使用numComma =(subt1.toString())。replace(「。」,「,」);現在和測試它,似乎工作... – Davyt