我有div
具有独特的id
我正在努力得到div
id
与 jQuery
<div class="quantity" id="UNIQUE_ID">Quantity</div>
一切正常,但加载后 div
与 ajax
- 我就是无法获取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/