0

如果在弹出 div 内单击,我希望弹出 div 保持打开状态,因为它包含要由用户填写的表单。这是我的代码

Javascript:

<script>
$('#btnFieldWorkers').bind("click", ToggleDisplay);
function ToggleDisplay() {
    if ($("#div_fieldWorkers").data('shown')) {
       hide();
    }
    else
       display();
 }

    function display() {
        if ($("#div_fieldWorkers").children().length > 0) {
            $("#div_fieldWorkers").fadeIn(300, function () {
                $(document).bind("click", function () { hide(); });
                $("#div_fieldWorkers").data('shown', true)
            });
        }
    }

    function hide() {
        $("#div_fieldWorkers").fadeOut(300, function () {
            $(document).unbind("click");
            $("#div_fieldWorkers").data('shown', false);
        });

    }
</script>

HTML:

<input type="button" value="Add Field Worker" id="btnFieldWorkers" onclick="return ToggleDisplay();" class="btn btn-primary" />
<div id="div_fieldWorkers" style="display:none;" class="form_size"></div>
4

4 回答 4

1

当用户在叠加层内单击时,您需要停止传播:

$("#div_fieldWorkers").on('click', function(e) {
    e.stopPropagation();
});
于 2013-09-30T12:18:36.663 回答
0

工作演示

尝试这个

     $('#btnFieldWorkers').bind("click", ToggleDisplay);

function ToggleDisplay(e) {
    e.stopPropagation();
    if ($("#div_fieldWorkers").data('shown')) {
        hide();
    } else display();
}

function display() {
    if ($("#div_fieldWorkers").children().length > 0) {
        $("#div_fieldWorkers").fadeIn(300, function () {
            $("#div_fieldWorkers").data('shown', true)
        });
    }
}

function hide() {
    $("#div_fieldWorkers").fadeOut(300, function () {
        $("#div_fieldWorkers").data('shown', false);
    });

}
$('body').on('click', function (e) {
    if ($(e.target).is(' :not(#div_fieldWorkers,#div_fieldWorkers *)')) {
        hide();
    }
});
于 2013-09-30T12:54:01.623 回答
0

经过大量搜索和帮助后,我发现这是有效的:

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

        if (e.target.parentElement.parentElement.parentElement == document.getElementById("div_fieldWorkers") || e.target == document.getElementById("btnFieldWorkers")) { }
        else
        {
            $("#div_fieldWorkers").slideUp("fast");
        }
    });
    function ToggleDisplay() {
        display();
    }

    function display() {

        $("#div_fieldWorkers").slideDown("fast");
    }   
于 2013-10-03T05:34:11.300 回答
0

从后面的代码中,您可以执行以下操作

 ClientScript.RegisterStartupScript(this.GetType(), "Popup", "display();", true);
于 2014-04-08T10:10:30.087 回答