简体   繁体   English

为什么当我删除仍执行onclick事件的html按钮时?

[英]why when i remove an html button still executing onclick event?

i´m trying to create a dynamically button with a jquery instruction append, but first i remove the content from the container div this all occurs when click an image, so the button created after that event should be repacing the las one, however, the problem is that the button erased still executing its own onlcick event , so i get multiple window open in my browser. 我正在尝试创建一个带有jquery指令追加的动态按钮,但是首先我从容器div中删除了内容,所有这些都在单击图像时发生,因此在该事件之后创建的按钮应该重新播放las,但是,问题在于,擦除的按钮仍在执行自己的onlcick事件,因此我在浏览器中打开了多个窗口。

i leave below the code , i hope some body can help to understand whats happening , by the way i don´t know why but the button is created inside a span tag in the DOM ... and then just inside the div container. 我把代码放在下面,我希望有人可以帮助我理解原因,但我不知道为什么,但是按钮是在DOM的span标记内创建的,然后在div容器内创建。

<script> 
    idDelegacion = 0;
    function planoClick(id) {
         idDelegacion = id;
         $.ajax({
             type: 'GET',
             contentType: "application/json; charset=utf-8",
             url: '@Url.Action("partialViewLuminariasDelegacion","Luminaria")',
             data: {
                 "idDelegacion": idDelegacion
             },
             datatype: "json",

             success: function (data) {

                 $("#luminariasDelegacion").html(data);
                 $("#nuevaLuminaria").show();
                 $("#luminariasDelegacion").show();
                 $("#nuevaLuminaria").empty();
                 //$("#nuevaLuminaria").live().ready(function () { $("#nuevaLuminaria").children().children("#btnNuevaLuminaria").remove(); });
                 $("#nuevaLuminaria").append('<input type="button"  value="Nueva Luminaria" id="btnNuevaLuminaria" style="position:relative"/>').button().click(function jsAbreOperacionLuminaria() {
                     url = "../../Luminaria/Create/?idDelegacion=" + idDelegacion
                     window.open(url);
                 });
             },
             error: function () {
                 alert('error al traer las luminarias de la delegación ' + idDelegacion)
             }
         });
     }
 </script>

The new button also fires the event because you are using live to bind the event, which is actually tied to the body and just listens for the target named nuevaLuminaria . 新按钮还会触发该事件,因为您正在使用live绑定该事件,该事件实际上与body绑定,并且仅侦听名为nuevaLuminaria的目标。 Before you replace the first button, call die on it to remove the listener. 更换第一个按钮之前,请在其上调用die以删除监听器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM