html - 获取 AJAX 加载内容的 DIV ID

标签 html jquery jquery-post

我有div具有独特的id我正在努力得到div idjQuery

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

一切正常,但加载后 divajax - 我就是无法获取id来自已加载div .

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

有什么想法吗?

最佳答案

这应该有效

$('.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);
    }); 
});     

关于html - 获取 AJAX 加载内容的 DIV ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17516572/

相关文章:

html - .inc 文件未可靠包含

javascript - 可在移动设备上关闭所有部分的 Accordion ,但在平板电脑和台式机上始终打开一个 Accordion 部分

jquery - 删除 on 时更改每个 div 上的背景颜色

php - 突出显示 PHP 中的新 mySQL 条目

javascript - 加载javascript时如何修复流体div标签

html - 如何根据它的宽度使用背景图像制作 div 的高度?

javascript - 即使我包含了他们指示我们做的所有库,Summernote 也无法工作

javascript - 如何让这个轮播每 'defined' 秒更换一次图像?

javascript - jQuery post 之后加载新的 javascript 文件的最佳方法是什么?

javascript - 是否可以在使用 Javascript/JQuery 发送到服务器之前将表单数据转换为文件?