1

私はdivユニークなものを持っていてid、それを手に入れようとしてdiv idいますjQuery

<div class="quantity" id="UNIQUE_ID">Quantity</div>

すべてがうまく機能しますが、ロード後divは-からロードajaxできません。iddiv

$('.quantity').click(function() {       
    $.post('quantity.php', { id: $(this).attr('id') }, function(output) {
        $(this).html(output);
    }); 
}); 

何か案は?

4

2 に答える 2

3

これはうまくいくはずです

$('.quantity').click(function() {
    var that = this;        

    $.post('quantity.php', { quantityId: $(that).attr('id') }, function(data) {
        $(that).html(data);
    }); 
}); 

しかし、これは私がそれを書く方法です

<div class="quantity" data-id='<?=$unique_id?>'>
    Quantity
</div>

$('.quantity').on('click', function() {
    var that = this;        

    $.post('quantity.php', { quantityId: $(that).data('id') }, function(data) {
        $(that).html(data);
    }); 
});     

そして動的divの場合

<div class="quantity" data-id='<?=$unique_id?>'>
    Quantity
</div>

$(document).on('click', '.quantity', function() {
    var that = this;        

    $.post('quantity.php', { quantityId: $(that).data('id') }, function(data) {
        $(that).html(data);
    }); 
});     
于 2013-07-07T21:54:16.010 に答える
1

div が更新されると、div へのonclickバインドは機能しません (document.ready() にバインドされていますよね?)。解決策は、変更するたびに関数を要素に再バインドするか(悪いもの)、のon()関数を使用することですjquery。コード例:

$(document).on('click', '.quantity', function(){
    var id = $(this).attr('id');        

    $.post('quantity.php', { quantityId: id }, function(data){
        $('#'+ id).html(data);
    }); 
}); 

更新:コメントで説明したようにon、非推奨のメソッドとして実際に機能するクラスではなく、メソッドをドキュメント全体にバインドする必要がありますlive()

于 2013-07-07T21:44:22.583 に答える