一直试图将以下内容转换为数字:
<button class="btn btn-large btn-info" data-votevalue="1">
<strong>1</strong>
</button>
var votevalue = parseInt($(this).data('votevalue'));
我也尝试过
Number()
,但在检查结果时仍会得到NaN
。我究竟做错了什么?这是完整的代码:
<div class="span7" id="button-group">
<div class="btn-group">
<button class="btn btn-large btn-info" data-votevalue="1"><strong>1</strong></button>
<button class="btn btn-large btn-info" data-votevalue="2"><strong>2</strong></button>
<button class="btn btn-large btn-info" data-votevalue="3"><strong>3</strong></button>
<button class="btn btn-large btn-info" data-votevalue="4"><strong>4</strong></button>
<button class="btn btn-large btn-info" data-votevalue="5"><strong>5</strong></button>
<button class="btn btn-large btn-info" data-votevalue="6"><strong>6</strong></button>
<button class="btn btn-large btn-info" data-votevalue="7"><strong>7</strong></button>
<button class="btn btn-large btn-info" data-votevalue="8"><strong>8</strong></button>
<button class="btn btn-large btn-info" data-votevalue="9"><strong>9</strong></button>
<button class="btn btn-large btn-info" data-votevalue="10"><strong>10</strong></button>
</div>
</div>
$('#button-group button').each(function() {
$(this).click(function() {
$(this).addClass('active');
var votevalue = parseInt($(this).data('votevalue'));
var filename = $('.mainimage').data('filename');
var votes = parseInt($('.mainimage').data('numvotes'));
var totalscore = parseInt($('.mainimage').data('totalscore'));
$.ajax({
type: 'POST',
url: 'index.php/?category=vote',
data: {
"votevalue": votevalue,
"filename": filename
},
success: function() {
votes++;
alert(votes);
var average = ((totalscore + votevalue) / votes);
$('#vote-incremenet').html(votes);
$('#display-average').html(average);
$('#display-average').show();
$('#button-group button').each(function(){
$(this).unbind('click');
});
}
}); // end ajax
}); // end click
}); // end each
最佳答案
听起来您的代码中的this
没有引用您的.btn
元素。尝试使用选择器显式引用它:
var votevalue = parseInt($(".btn").data('votevalue'), 10);
另外,不要忘记基数。