看看这个脚本…运行,看看奇怪..
来自jsfiddle
var m = 'Jan07'; var mm = 'Jan'; alert(m.replace(mm,'')); alert(parseInt(m.replace(mm,''))); var m = 'Jan08'; var mm = 'Jan'; alert(m.replace(mm,''))); var m = 'Jan09'; var mm = 'Jan'; alert(m.replace(mm,''))); var m = 'Jan10'; var mm = 'Jan'; alert(m.replace(mm,'')));
解决方法
这是一个八进制问题:尝试parseInt(val,10).领先的零使它相信它是八进制的.
parseInt采用第二个可选参数radix:
radix An integer that represents the radix of the above mentioned
string. While this parameter is optional,always specify it to
eliminate reader confusion and to guarantee predictable behavior.
Different implementations produce different results when a radix is
not specified.
所以:
parseInt('09') // 0 parseInt('09',10); // 9