Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在jquery on()函数中获取childSelector_Jquery - Fatal编程技术网

如何在jquery on()函数中获取childSelector

如何在jquery on()函数中获取childSelector,jquery,Jquery,我使用Jquery动态添加列表元素(html li),并尝试创建一个函数来删除它:以下是我的html代码: <ul id="tags_liste" class="list-inline"> <li class="tags"> <a href="" title=""> <span class="label label-primary">Tag 1</span> </a> <a

我使用Jquery动态添加列表元素(html li),并尝试创建一个函数来删除它:以下是我的html代码:

<ul id="tags_liste" class="list-inline">
  <li class="tags">
    <a href="" title="">
      <span class="label label-primary">Tag 1</span>
    </a>  
    <a href="" title="Delete this tag" class="form_ajout_tag_suppr">
      <span class="glyphicon glyphicon-remove"></span>
    </a>
  </li>
  <li class="tags">
    <a href="" title="">
      <span class="label label-primary">Tag 2</span>
    </a>
    <a href="" title="Deletethis tag" class="form_ajout_tag_suppr">
      <span class="glyphicon glyphicon-remove"></span>
    </a>
  </li>
</ul>
s此
变量为空,因为当然没有id,所以我想获取单击哪个li元素以使用$.ajax成功块中的代码
$this.parent().remove()
删除它

也许我没有为jquery on()函数使用正确的方法

提前感谢您的帮助

删除
attr('id')
,只使用jQuery对象

$(“#标记列表”)。在(“单击“,”上。在函数(e)上形成标记支持{
e、 预防默认值();
var sThis=$(本);
if(确认(“是否确实要删除此标记?”){
$.ajax({
...
数据:{
斯莱贝尔:雄鹿
},
成功:功能(数据、状态){
如果(数据==“正常”){
sThis.parent().remove();
}否则{
返回false;
}
},
错误:函数(resultat、statut、erreur){
var s=“发生错误”;
控制台日志;
},
})
}
});

在哪里使用
sThis
,我可以看到一个
sTag
,但没有
sThis
(在分配变量后,除了
控制台.log()
);另外:为什么您需要
id
,您已经在
this
(DOM)和
$(this)
(jQuery对象)中引用了单击的元素?从单击的元素中,要访问祖先
  • ,只需使用
    this.closest('li')
    (兼容浏览器中的DOM)或
    $(this.closest('li')
    (jQuery)。
    $("#tags_liste").on("click", ".form_ajout_tag_suppr", function(e) {
      e.preventDefault();
    
      var sThis = $(this).attr("id");
    
      console.log("sThis : " + sThis);
    
      if (confirm("Do you really want to delete this tag?")) {
        $.ajax({
          type: "GET",
          url: "../../bobookmarks/content/json/delete_tag_in_session.php",
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          data: {
            sLibelle: sTag
          },
          success: function(data, statut) {
            if (data == "ok") {
              $(this).parent().remove();
            } else {
    
              return false;
            }
          },
          error: function(resultat, statut, erreur) {
            var s = "An error has occured";
            console.log(s);
          },
        })
      }
    });