Javascript警报号码从0开始 [英] Javascript alert number starting with 0

查看:68
本文介绍了Javascript警报号码从0开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮,在后面的代码中我添加了 onclick ,我传递了一个唯一的ID,它将被传递给js函数。 id以0开头。

I have a button where in the code behind I add a onclick and I pass a unique ID which will be passed to the js function. The id starts with a 0.

它无法正常工作,最终我发现数字,ID,传递错误...

It wasn't working and eventually I figured out that the number, id, it was passing was wrong...

即。看到这个: js fiddle

它适用于'在数字的开头和结尾。只是想知道为什么013转到11.我做了一些谷歌搜索,找不到任何东西......

It works with a ' at the start and end of the number. Just wondering why 013 turns to 11. I did some googling and couldn't find anything...

干杯

Robin

编辑:

谢谢大家。是的,现在明白了。

Thanks guys. Yep understand now.

在这种情况下,开头的0有意义,这里是邮件列表中的收件人ID,我将使用'013'而不是013,即a串。然后,我可以将值分成js,因为3个值中的每一个代表一个不同的id,它总是只有1个字符长,即0-9。

As in this case the 0 at the start has a meaning, here the recipient ID in a mailing list, I will use '013' instead of just 013, i.e. a string. I can then split the values in js as each of the 3 values represents a different id which will always be only 1 character long, i.e. 0-9.

推荐答案

0 开头的数字文字被视为八进制数。所以基数8中的13是基数10中的11 ...

A numeric literal that starts with a 0 is treated as an octal number. So 13 from base 8 is 11 in base 10...

八位数字文字已被弃用,但如果你没有处于严格模式,它仍然可以工作。

Octal numeric literals have been deprecated, but still work if you are not in strict mode.

(你没有问,但是)以 0x 开头的数字文字被视为十六进制。

(You didn't ask, but) A numeric literal that starts with 0x is treated as hexadecimal.

更多信息,请访问 MDN

在您的演示中,参数名为 id ,这意味着你不要需要对它进行数值运算 - 如果是这样,只需将其放在引号中并将其用作字符串。

In your demo the parameter is called id, which implies you don't need to do numerical operations on it - if so, just put it in quotes and use it as a string.

如果你需要能够传递一个前导零但仍然将数字视为基数10来对其进行数值运算,您可以将其括在引号中以将其作为字符串传递,然后将字符串转换为强制基数为10的数字,例如:

If you need to be able to pass a leading zero but still have the number treated as base 10 to do numerical operations on it you can enclose it in quotes to pass it as a string and then convert the string to a number in a way that forces base 10, e.g.:

something('013');

function something(id){    
    alert(+id);             // use unary plus operator to convert
    // OR
    alert(parseInt(id,10)); // use parseInt() to convert        
}

演示: http://jsfiddle.net/XYa6U/5/

这篇关于Javascript警报号码从0开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆