如果在弹出div中单击,弹出div仍保持打开状态

时间:2013-09-30 12:15:09

标签: javascript jquery

我希望popup 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 个答案:

答案 0 :(得分:1)

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

$("#div_fieldWorkers").on('click', function(e) {
    e.stopPropagation();
});

答案 1 :(得分:0)

工作DEMO

试试这个

     $('#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();
    }
});

答案 2 :(得分: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");
    }   

答案 3 :(得分:0)

从后面的代码中你可以像下面这样做

 ClientScript.RegisterStartupScript(this.GetType(), "Popup", "display();", true);