Javascript 在parseInt()上返回NaN
在上面的示例中,我有3个按钮,橙色、绿色和红色 悬停时,每个按钮显示一个[-]和一个[+]按钮,其中包含一类Javascript 在parseInt()上返回NaN,javascript,jquery,parseint,Javascript,Jquery,Parseint,在上面的示例中,我有3个按钮,橙色、绿色和红色 悬停时,每个按钮显示一个[-]和一个[+]按钮,其中包含一类btn\u动作 单击我试图从目标颜色按钮获取数值,然后将其递增或递减1 目前我在parseInt函数中得到NaN: $('.btn_action').unbind('click').bind('click', function() { var type = $(this).data('type').toString(); console.log(color); c
btn\u动作
单击我试图从目标颜色按钮获取数值,然后将其递增或递减1
目前我在parseInt函数中得到NaN:
$('.btn_action').unbind('click').bind('click', function() {
var type = $(this).data('type').toString();
console.log(color);
console.log(type);
// this is returning NaN, it should be returning 1:
var number = parseInt($('option_'+color).text(), 10);
console.log(number);
if (type === 'plus') {
// number = number + 1;
$('option_'+color).text(number + 1);
console.log(number);
}
});
关于我哪里出错的想法?你应该在课堂上使用
var number = parseInt($('.option_'+color).text(), 10);
但是您应该使用一元+
通常比parseInt更有效
var number = +$('.option_'+color).text();
您只需要更新option元素的jQuery选择器。正如莱昂所说 使用
$('.option\u'+color).text()代替$('option\u'+color).text()
我在查看小提琴示例时对其进行了修改: