简体   繁体   English

在其外部单击时关闭弹出式div

[英]Close popup div when clicked outside it

I have a button on which when i click, a form opens to be filled by the user. 我有一个按钮,当我单击该按钮时,将打开一个表单以供用户填写。 When i click the button again the form closes. 当我再次单击按钮时,表格关闭。 This wasn't possible as with one button i wasn't able to perform 2 functions like opening and closing of the form with one button. 这是不可能的,因为只有一个按钮我无法执行两个功能,例如用一个按钮打开和关闭表单。 So, i took 2 buttons. 因此,我按了2个按钮。 When one button is clicked, it hides and the second button shows and the form is opened and when the second button is clicked, the same button hides and the first button shows and the form closes. 单击一个按钮时,它会隐藏并显示第二个按钮,然后打开表单;单击第二个按钮时,会隐藏相同的按钮,并显示第一个按钮,然后关闭表单。 I have no problem in this. 我没有这个问题。 Now, I also want that when user clicks anywhere outside the div form, the form should close itself. 现在,我还希望当用户单击div表单之外的任何位置时,该表单应自行关闭。 Help me do this. 帮帮我 These are the 2 buttons. 这是两个按钮。

      <input type="button" value="Add New" id="NewRow" class="btn1 green" onclick="ToggleDisplay()" />
            <input type="button" value="Add New" id="NewRowCopy" style="display: none;" class="btn green" />

This is the form. 这是表格。

    <div id="div_fieldWorkers" style="display:none;" class="formsizeCopy">

This is the script. 这是脚本。

        $(document).ready(function () {
        $('#div_fieldWorkers').hide();
        $('input#NewRowCopy').hide();  });

  $('input#NewRow').click(function () {
        $('input#NewRowCopy').show();
        $('input#NewRow').hide();
        $('#div_fieldWorkers').slideDown("fast");
    });
    $('input#NewRowCopy').click(function () {
        $('input#NewRow').show();
        $('input#NewRowCopy').hide();
        $('#div_fieldWorkers').slideUp("fast");


    });

    $("html").click(function (e) {

        if (e.target.parentElement.parentElement.parentElement == document.getElementById("div_fieldWorkers") || e.target == document.getElementById("NewRow")) {

        }
        else
        {
            $("#input#NewRowCopy").hide();
            $("#input#NewRow").show();
            $("#div_fieldWorkers").slideUp("fast");
       }

I an trying to hide the second button when clicked outside the form.. but not working.. 我试图在窗体外部单击时隐藏第二个按钮。但是不起作用。

Try 尝试

$(document).ready(function () {
    $('#div_fieldWorkers').hide();
    $('input#NewRowCopy').hide();  

    $('#NewRow').click(function (e) {
        $('#NewRowCopy').show();
        $('#NewRow').hide();
        $('#div_fieldWorkers').stop(true, true).slideDown("fast");
        e.stopPropagation();
    });
    $('#NewRowCopy').click(function (e) {
        $('#NewRow').show();
        $('#NewRowCopy').hide();
        $('#div_fieldWorkers').stop(true, true).slideUp("fast");
        e.stopPropagation();
    });

    $(document).click(function (e) {
        var $target = $(e.target);
        if ($target.closest('#div_fieldWorkers').length == 0) {
            $("#NewRowCopy").hide();
            $("#NewRow").show();
            $("#div_fieldWorkers").stop(true, true).slideUp("fast");
        }
    })
});

Demo: Fiddle 演示: 小提琴

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

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