我在页面中嵌入了以下 JS:
var round = Math.round;
var id = $(this).attr("id");
var len = id.length;
var indexPos = len -1; // index of the number so that we can split this up and used it as a title
var pasType = id.substring(0, indexPos); // adult, child or infant
var ind = round(id.substring(indexPos)); // converts the string index to an integer
var number = (id.substring(indexPos) + 1); // creates the number that will go in the title
window.alert(number);
id
会是类似 Adult0 的东西,我需要将该字符串拆分为 Adult 和 0 - 这部分工作正常。
当我尝试增加 0 时,问题就出现了。如您所见,我使用 Math.round 将其转换为整数,然后向其添加 1 - 我希望在此之后 0 变为 1。但是,它似乎没有将其转换为整数,因为我得到01
, not 1
。当用 Adult1 测试这个时,我收到的警报是11
.
我在用着this https://stackoverflow.com/q/1133770/1073118供参考的问题,也尝试过var number += id.substring(indexPos);
,这破坏了 JS (unexpected identifier '+='
)
有谁知道我做错了什么?有更好的方法吗?
The parseInt()
函数解析一个string
并返回一个integer
,10 是基数或基数[DOC] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
var number = parseInt(id.substring(indexPos) , 10 ) + 1;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)