这是我的代码:
HTML 代码:(由 ajax 动态生成)。
<div class="btn btn-primary" image-data="imagedata" name-data="name data" artist-data="artist data" country-data="in" id="1252785363" onclick="ajax($(this));return false;">Add Album</div>
<div class="btn btn-primary" image-data="imagedata" name-data="name data" artist-data="artist data" country-data="in" id="1252985363" onclick="ajax($(this));return false;">Add Album</div>
<div class="btn btn-primary" image-data="imagedata" name-data="name data" artist-data="artist data" country-data="in" id="1256985363" onclick="ajax($(this));return false;">Add Album</div>
每个 DIV 都有唯一的 ID!
Javascript:
function ajax($this) {
var id = $this.attr("id");
var image_data = $this.attr("image-data");
var name_data = $this.attr("name-data");
var artist_data = $this.attr("artist-data");
var country_data = $this.attr("country-data");
$.ajax({
method: "POST",
url: "URL to Call",
dataType: "text",
data: {
image: image_data,
artist: artist_data,
name: name_data,
country: country_data,
id: id
},
success: function(response) {
console.log(response);
$(id).hide();
alert(id);
}
});
}
它返回响应,它提醒 ID,但不知何故我找不到,我尝试 $(id).css('display', 'none');
,切换但没有好运!
如果可能,请告诉我解决方案!
最佳答案
您需要使用#
前缀来选择一个ID。
$("#" + id).hide();
关于javascript - Ajax生成的内容,onclick时无法隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44979702/