这是我正在尝试使用的功能
$('body').on('click', '.up-vote', function(event) {
event.preventDefault();
var data = {"action": "voteKarma", "id": $(this).data("id"), "value": $(this).data("val")}
$.post(window.apiURL, data, function(result) {
switch(result['status']) {
case 1:
var vote_sum_text = $(this).next(".sum").text();
if (!isNaN(parseInt(vote_sum_text, 10))) {
var vote_sum_text = $(this).next(".sum").text();
} else { alert("isNaN Variable") }
break;
}, 'json');
});
当 Ajax 结果返回 0 时,它会返回一个带有 isNaN 变量 的警报,这是我调试问题的后备方案。
这是我的 HTML 布局,它是使用另一个 Ajax 请求动态抓取的,<li>
中列出了多个这样的 div。格式:
<div class="votes pull-left">
<a class="up-vote" href="#" data-id="20" data-val="1"><span class="fa fa-arrow-circle-o-up"></span></a>
<span class="sum" style="font-weight:400;color:#666;">
0
</span>
<a class="down-vote" href="#" data-id="20" data-val="0"><span class="fa fa-arrow-circle-o-down"></span></a>
</div>
简单来说;当你点击 .up-vote
或 .down-vote
它会发送一个 AJAX 请求,然后获取 text()
的 .sum
值(value)。
最佳答案
尝试使用
$(event.currentTarget).next(".sum").text();
因为 .post
中的 this
没有引用元素
关于javascript - isNan 在 jQuery 中使用此选择器和动态内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28024703/