0

私は以下のようなHTMLスニペットを持っています

var useful, cool, funny;

'<ul data-component-bound="true" class="voteset'+data[i]['review_id']+'">'+
    '<li class="useful ufc-btn" id="1">'+
      '<a href=javascript:vote('+data[i]['review_id']+',"useful") rel="useful"><span>Useful</span></a>'+
        '<span>'+useful+'</span>'+
    '</li>'+
    '<li class="funny ufc-btn" id="2">'+
      '<a href=javascript:vote('+data[i]['review_id']+',"funny") rel="funny"><span>Funny</span></a>'+
        '<span>'+funny+'</span>'+
    '</li>'+
    '<li class="cool ufc-btn" id="3">'+
      '<a href=javascript:vote('+data[i]['review_id']+',"cool") rel="cool"><span>Cool</span></a>'+
        '<span>'+cool+'</span>'+
    '</li>'+
    '<span class="vote'+data[i]['review_id']+'"></span></ul>'+
'</div>';

各レビューには、便利でクールで面白い3つのボタンがあります。クリック操作を行ったときに、各ボタン内のカウントを増やすにはどうすればよい<span>ですか?以下は試しましたが機能しません。ありがとうございます。

function vote(reviewId,status) {
    $('.vote'+reviewId).text('posting ...');
    $('.voteset'+reviewId).click(function(){
      var updatevote = $(this).find("li>span").text();
      updatevote = updatevote + 1;
      $(this).next('span').html(updatevote);
    });
}
4

4 に答える 4

2

parseIntNaN文字列が空の場合は問題が発生しますが""、代わりに試してくださいMath.floor

function vote(reviewId,status)
{

    $('.vote'+reviewId).text('posting ...');
    $('.vote'+reviewId).text('posting ...');
        var $span = $('.voteset' + reviewId + ' li.' + status + ' span:eq(1)');
        $vote = Math.floor($span.text()) + 1; 
        $span.html($vote);
}
于 2012-10-16T03:37:19.290 に答える
1

あなたupdatevoteは数字ではなくテキストとして解釈されます。試す

var updatevote = parseInt($(this).find("li>span").text(), 10);
于 2012-10-15T20:45:06.550 に答える
0

これを試して

var updatevote = +$(this).find("li>span").text(); // Convert to number

updatevote++;

また$(this).find("li>span") 、li内のすべてのスパンを含むオブジェクトが表示されます。3つの異なるli内に3つあることがわかります。

どのように処理していますか。

于 2012-10-15T20:46:10.710 に答える
0

ここにイベントバインディングに一般的な問題があります

function vote(reviewId,status) {
    $('.vote'+reviewId).text('posting ...');
    $('.voteset'+reviewId).click(function(){ // <-don't need this since you call vote directly from a#href
      var updatevote = $(this).find("li>span").text();
      updatevote = updatevote + 1;
      $(this).next('span').html(updatevote);
    });
}

これは機能するはずです:

function vote(reviewId,status) {
    $('.vote'+reviewId).text('posting ...');

    var $span = $('.voteset' + reviewId + ' li.' + status + ' span');
    var updatevote = parseInt($span.text(), 10);

    updatevote = updatevote + 1;
    $span.text(updatevote);

}
于 2012-10-15T20:55:13.193 に答える