带有前导零的数字在JavaScript中会发生变化 [英] Number with leading zeroes gets changed in JavaScript
问题描述
我使用print语句打印出html代码,并从php调用javascript函数,并传入一个整数。但是,它在php中传递的值与javascript函数收到的数字不匹配,我不知道为什么。
以下是调用javascript函数:
$ upc = $ items [$ i] - > GetUPC();
打印upc:。 $ upc。 <峰; br /> 中;
$ delete =< a href ='#'onclick ='removeFromCart(。$ upc。)'>< img src ='。$ redx。'height = '17'width = '17'/>< / a>;
print语句打印出值:0011110416605
< a href ='# 'onclick ='removeFromCart(0011110416605)'>
这是被调用的javascript函数:
function removeFromCart(itemID){
var option = document.getElementById(select_cart);
var selected = option.options [option.selectedIndex] .value;
alert(itemID);
loadCart(itemID,selected,,remove);
}
警告框正在输出数字:1226972549
我能理解它是否正在删除前导0,但这个数字完全不同。有谁知道这可能是什么原因?
JavaScript认为它是一个八进制值(由于前导零和缺少数字大于 7
)。八进制 0011110416605
的十进制值为 1226972549
。示例:
> var值= 010; // 10 in octal
>的console.log(值);
> 8 //十进制8是
改为使用字符串:
removeFromCart(0011110416605)
I am calling a javascript function from php using a print statement to print out html code, and I am passing in an integer. However, the value when it is passed in php is not matching the number that the javascript function receives, and I have no clue why.
Here is the php that calls the javascript function:
$upc = $items[$i]->GetUPC();
print "upc: " . $upc . "<br/>";
$delete = "<a href='#' onclick='removeFromCart(". $upc .")' ><img src='".$redx."' height='17' width='17' /></a>";
The print statement prints out the value: 0011110416605
And on the page, using view source, here is the tag for this:
<a href='#' onclick='removeFromCart(0011110416605)' >
Here is the javascript function that is being called:
function removeFromCart(itemID){
var option = document.getElementById("select_cart");
var selected = option.options[option.selectedIndex].value;
alert(itemID);
loadCart(itemID,selected,"","remove");
}
The alert box is printing out the number: 1226972549
I could understand if it was removing the leading 0's, but this number is completely different. Does anyone know why this may be?
JavaScript thinks it is an octal value (because of the leading zero and the lack of digits greater than 7
). The decimal value of octal 0011110416605
is 1226972549
. Example:
> var value = 010; //10 in octal
> console.log(value);
> 8 //is 8 in decimal
Use a string instead:
removeFromCart("0011110416605")
这篇关于带有前导零的数字在JavaScript中会发生变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!