Javascript isNan在jQuery中使用此选择器和动态内容
这是我尝试使用的函数Javascript isNan在jQuery中使用此选择器和动态内容,javascript,jquery,ajax,Javascript,Jquery,Ajax,这是我尝试使用的函数 $('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) { switc
$('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请求动态捕获的。
格式中列出了多个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>
0
简而言之,;当您单击.up vote
或down vote
时,它将发送一个AJAX请求,然后获取.sum
值的文本()
$(event.currentTarget).next(".sum").text();
因为.post
中的此
不涉及元素,所以您也可以使用以下内容:
$('body').on('click', '.up-vote', function(event) {
event.preventDefault();
var up_vote_this = $(this);
....
....
//now you can use the variable in the success function...
var vote_sum_text = up_vote_this.next(".sum").text();