简体   繁体   English

在Jquery中冒起的Click事件

[英]Click event bubbling up in Jquery

I'm developing a project using Django 1.6. 我正在使用Django 1.6开发项目。 I have a pop up box which pops up when I click "Quick Upload" and there I'll be giving the path of a folder and say "OK".But the problem is when I click "OK" for one time then "function event()" is being called 4 times. 我有一个弹出框,当我单击“快速上载”时会弹出,然后在其中给出文件夹的路径并说“确定”。但是问题是当我单击“确定”一次然后单击“功能”时event()”被调用了4次。 Please help me in this regard. 请帮助我这方面。 Code is shown below. 代码如下所示。

<input type="hidden" id="name" name="name" value={{name}}>
<input id="button" onclick="quickupload();" class="css_button" value="Quick Upload">

<script>
function quickupload()
{
    $('div#dialog-confirm').dialog('open'); 
    $( "#dialog-confirm" ).dialog
    ({
      resizable:true,
      height:150,
      modal: true,
      buttons: 
      {
        "OK": function event()
        { 
        var session1 = $("#name").val();
        var fpath = $("#quick1").val();
        listofpath=fpath.split("\\");
        finalpath="";   
        for (i=0;i<listofpath.length;i++)
        {
            finalpath=finalpath+listofpath[i]+"@";          
        }           
        console.log(finalpath);
        window.location.href = "/uploadquick/"+session1+"/"+finalpath;
        },
      }
    });
}
</script> 

You're creating a dialog object every time you call quickupload() . 每次调用quickupload()时,您都在创建一个对话框对象。 So the same handler you passed to OK in dialog definition is attached again and again on OK button every time you click on that Quick Upload button. 因此,每次单击该Quick Upload按钮时,在对话框定义中传递给OK的同一处理程序都会一次又一次地附加在OK按钮上。 Instead create a dialog object outside and then open on it you button click like this 而是在外部创建一个对话框对象,然后在其上打开,然后单击按钮,如下所示

<script>
// create the dialog object outside
$( "#dialog-confirm" ).dialog({
      resizable:true,
      height:150,
      modal: true,
      buttons: 
      {
        "OK": function event()
        { 
        var session1 = $("#name").val();
        var fpath = $("#quick1").val();
        listofpath=fpath.split("\\");
        finalpath="";   
        for (i=0;i<listofpath.length;i++)
        {
            finalpath=finalpath+listofpath[i]+"@";          
        }           
        console.log(finalpath);
        window.location.href = "/uploadquick/"+session1+"/"+finalpath;
        },
      }
    });

function quickupload() {
    $('#dialog-confirm').dialog('open'); 
}
</script> 

Hope this helps :) 希望这可以帮助 :)

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

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