Am I right in thinking these numbers are the IDs / reference numbers of something-or-other? If so, then it's more appropriate to store them as strings instead of numbers:
<input type="button" onclick="sample('00119001')" value="00119001" /><br/>
Вопрос
I have items in list with 8-digits and most of them begin with two zeros. When I want to call function and pass it's unique number, almost always when it starts with "00" it gets some seemingly random numbers. I still don't know how or why it is happening. Here is the example on JSFiddle.
HTML:
<div id="working">
<input type="button" onclick="sample(00119001)" value="00119001" /><br/>
<input type="button" onclick="sample(00113008)" value="00113008" /><br/>
<input type="button" onclick="sample(68745696)" value="68745696" /><br/>
<input type="button" onclick="sample(11112222)" value="11112222" /><br/>
</div>
<div id="notworking">
<input type="button" onclick="sample(00113004)" value="00113004" /><br/>
<input type="button" onclick="sample(00113003)" value="00113003" /><br/>
<input type="button" onclick="sample(00106002)" value="00106002" /><br/>
<input type="button" onclick="sample(00120003)" value="00120003" /><br/>
</div>
<div id="show"></div>
JS:
function sample(a) {
var b = "0000000"+a;
b = b.substr(b.length-8);
document.getElementById("show").innerHTML="Input: "+a+" | Output: "+b;
}
To clarify the problem: I expect to get the same number in a function that was used in argument to call it, but for some reason, some times I get totally different number.
Решение 2
Am I right in thinking these numbers are the IDs / reference numbers of something-or-other? If so, then it's more appropriate to store them as strings instead of numbers:
<input type="button" onclick="sample('00119001')" value="00119001" /><br/>
Другие советы
00113004
is interpreted as octal. This doesn't happen when you have a 8 or a 9 in your literal because the js engine guesses that it can't be octal.
To make a decimal literal, remove the 0
at the start
<input type="button" onclick="sample(113004)" value="00113004" /><br/>
If you really want to have 0, use a string :
<input type="button" onclick="sample('00113004')" value="00113004" /><br/>
function sample(a) {
var b = "0000000"+Number(a);
b = b.substr(b.length-8);
document.getElementById("show").innerHTML="Input: "+a+" | Output: "+b;
}