Javascript ajax成功后删除最近的li
我有一个div,列出了所有上传的文件Javascript ajax成功后删除最近的li,javascript,jquery,ajax,Javascript,Jquery,Ajax,我有一个div,列出了所有上传的文件 $i = 0; echo '<div style="float: left;margin-left: 25px;" id="filecontainer">'; echo "<ul>"; foreach($editorderdata['uploadedfiles'] as $row){ echo '&l
$i = 0;
echo '<div style="float: left;margin-left: 25px;" id="filecontainer">';
echo "<ul>";
foreach($editorderdata['uploadedfiles'] as $row){
echo '<li>';
echo '<a href="'.base_url().'images/userfiles/'.$row['filename'].'" target="_blank">';
echo 'file_'.++$i.'</a>';
echo '<a href="#" data-fileid="'.$row['filename'].'" title="Remove file" class="removefile">';
echo '<img class="cross" src="'.base_url().'images/cross.png">';
echo '</a></li>';
}
echo "</ul>";
echo "</div>";
$i=0;
回声';
回声“”;
foreach($editorderdata['uploadedfiles']作为$row){
回音“- ”;
回声';
回音“
”;
}
回声“
”;
回声“;
这是按.removefile类元素删除所选文件的代码
$(document).ready(function() {
$(".removefile").click(function(e) {
e.preventDefault();
var fileidvar = $(this).data('fileid');
if (confirm("Are you sure you want to remove selected file?")) {
$.ajax({
type: "POST",
url:'<?php echo base_url() ?>signup/removefile',
data:{ fileid: fileidvar },
dataType: 'json',
success: function (data) {
if(data.value){
alert(data.value);
$(this).closest("li").remove();
}
},
error:function(){
alert("Something went wrong, please try again.");
}
});
}
return false;
});
});
$(文档).ready(函数(){
$(“.removefile”)。单击(函数(e){
e、 预防默认值();
var fileidvar=$(this).data('fileid');
如果(确认(“是否确实要删除所选文件?”){
$.ajax({
类型:“POST”,
url:'signup/removefile',
数据:{fileid:fileidvar},
数据类型:“json”,
成功:功能(数据){
if(data.value){
警报(数据值);
$(this).最近的(“li”).remove();
}
},
错误:函数(){
警报(“出现问题,请重试”);
}
});
}
返回false;
});
});
代码运行良好,但我想删除的是ajax success上的父li,这不起作用。。帮助?试试看
if (confirm("Are you sure you want to remove selected file?")) {
var self=this;// make a copy of this to self and use it in ajax function
$.ajax({
type: "POST",
url:'<?php echo base_url() ?>signup/removefile',
data:{ fileid: fileidvar },
dataType: 'json',
success: function (data) {
if(data.value){
alert(data.value);
$(self).closest("li").remove();// use self in place of this
}
},
error:function(){
alert("Something went wrong, please try again.");
}
});
}
if(确认(“是否确实要删除所选文件?”){
var self=this;//将其复制到self并在ajax函数中使用
$.ajax({
类型:“POST”,
url:'signup/removefile',
数据:{fileid:fileidvar},
数据类型:“json”,
成功:功能(数据){
if(data.value){
警报(数据值);
$(self).最近的(“li”).remove();//使用self代替此
}
},
错误:函数(){
警报(“出现问题,请重试”);
}
});
}
您遇到的问题是$(此)
在success
处理程序中与单击的元素不相等。试试这个:
$(".removefile").click(function(e) {
e.preventDefault();
var $btn = $(this); // <- save the clicked button to a variable
var fileidvar = $btn.data('fileid');
if (confirm("Are you sure you want to remove selected file?")) {
$.ajax({
type: "POST",
url:'<?php echo base_url() ?>signup/removefile',
data: { fileid: fileidvar },
dataType: 'json',
success: function (data) {
if (data.value) {
alert(data.value);
$btn.closest("li").remove(); // <- use cached selector here
}
},
error: function() {
alert("Something went wrong, please try again.");
}
});
}
return false;
});
$(“.removefile”)。单击(函数(e){
e、 预防默认值();
var$btn=$(此);//问题$(此)
不是单击的元素
因此我们将选择器缓存在这里var this1=$(this);
$(“.removefile”)。单击(函数(e){
var this1=$(本);
e、 预防默认值();
var fileidvar=$(this).data('fileid');
如果(确认(“是否确实要删除所选文件?”){
$.ajax({
类型:“POST”,
url:“注册/删除文件”,
数据:{
fileid:fileidvar
},
数据类型:“json”,
成功:功能(数据){
if(data.value){
警报(数据值);
这1.最近的(“li”)。删除();
}
},
错误:函数(){
警报(“出现问题,请重试”);
}
});
}
返回false;
});
尝试更改:
$(this).closest("li").remove();
到
像这样试试
$(".removefile").click(function(e) {
e.preventDefault();
var this = $(this);
var fileidvar = $(this).data('fileid');
if (confirm("Are you sure you want to remove selected file?")) {
$.ajax({
type: "POST",
url:'<?php echo base_url() ?>signup/removefile',
data:{ fileid: fileidvar },
dataType: 'json',
success: function (data) {
if(data.value){
alert(data.value);
this.closest("li").remove();
}
},
error:function(){
alert("Something went wrong, please try again.");
}
});
}
return false;
});
$(“.removefile”)。单击(函数(e){
e、 预防默认值();
var this=$(this);
var fileidvar=$(this).data('fileid');
如果(确认(“是否确实要删除所选文件?”){
$.ajax({
类型:“POST”,
url:'signup/removefile',
数据:{fileid:fileidvar},
数据类型:“json”,
成功:功能(数据){
if(data.value){
警报(数据值);
此。最近的(“li”)。删除();
}
},
错误:函数(){
警报(“出现问题,请重试”);
}
});
}
返回false;
});
希望这有帮助没问题,很乐意帮忙。
$(this).parent().remove();
$(".removefile").click(function(e) {
e.preventDefault();
var this = $(this);
var fileidvar = $(this).data('fileid');
if (confirm("Are you sure you want to remove selected file?")) {
$.ajax({
type: "POST",
url:'<?php echo base_url() ?>signup/removefile',
data:{ fileid: fileidvar },
dataType: 'json',
success: function (data) {
if(data.value){
alert(data.value);
this.closest("li").remove();
}
},
error:function(){
alert("Something went wrong, please try again.");
}
});
}
return false;
});