0

我有pop.jsp两个 div 标签,当我点击时,FindByid只能通过 id 字段显示找到,如果我点击,findByname则该字段仅显示完美工作。但问题是,当我打开一个弹出窗口时,有时没有该字段可见或有时它可以顺利工作我不知道为什么会发生我想相应地在单击按钮时看到相应的字段

注意有时div标签的可见性仅适用于 internet explorer其他浏览器没有div标签可见所以请解决我的问题

这是我的 index.jsp 弹出代码,我通过它弹出

<script type="text/javascript">
function windowLoadByName(windowHeight, windowWidth) {
    var centerWidth = (window.screen.width - windowWidth) / 2;
    var centerHeight = (window.screen.height - windowHeight) / 2;

    newWindow = window.open(
            'PopUp.jsp',
            'mywindow',
            'resizable=0,width=' + windowWidth + ',height=' + windowHeight
                    + ',left=' + centerWidth + ',top=' + centerHeight,
            "status=1").divHiding(1);
    newWindow.focus();

}
function windowLoadById(windowHeight, windowWidth) {
    var centerWidth = (window.screen.width - windowWidth) / 2;
    var centerHeight = (window.screen.height - windowHeight) / 2;
    newWindow = window.open(
            'PopUp.jsp',
            'mywindow',
            'resizable=0,width=' + windowWidth + ',height=' + windowHeight
                    + ',left=' + centerWidth + ',top=' + centerHeight)
            .divHiding(2);

    newWindow.focus();
    return newWindow.name;
}

这是我的 pop.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://jakarta.apache.org/taglibs/input-1.0"
    prefix="input"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript">
    function loadByName() {
        var l = document.getElementById("firstName");
        var v = l.value;
        window.opener.loadName(v);
        window.close();
    }
    function loadById() {
        var ids = document.getElementById("id");
        var v = ids.value;
        window.opener.loadId(v);
        window.close();
    }

    function divHiding(id) {

        var div1 = document.getElementById('div1');
        var div2 = document.getElementById('div2');


        // Check what the value of the button pressed and displays the correct div
        if (id == 1)
            div1.style.display = 'block';
        else if (id == 2) {
            div2.style.display = 'block';
        }
    }
</script>
</head>
<body>

    <form name="employeeForm" method="get">
        <div id="div1" style="display: none;">
            Employee Name:
            <center>
                <input type="text" name="firstName" id="firstName" />
            </center>
            <center>
                <input type="submit" name="Search" value="Search"
                    onclick="loadByName()" />
            </center>
        </div>
        <div id="div2" style="display: none;">
            Employee Id:
            <center>
                <input type="text" name="firstName" id="id" />
            </center>
            <center>
                <input type="submit" name="Search" value="Search"
                    onclick="loadById()" />
            </center>
        </div>
    </form>

</body>

</html>
4

1 回答 1

0

问题是因为您在 window.open(..) 中调用了 divHiding 函数,并且调用该函数时页面可能没有加载,这就是有时它起作用有时不起作用的原因。我建议的解决方案是通过 url 参数传递要显示的 div:

newWindow = window.open(
            'PopUp.jsp?div=1',
            'mywindow',
            'resizable=0,width=' + windowWidth + ',height=' + windowHeight
                    + ',left=' + centerWidth + ',top=' + centerHeight)

在 popup.jsp 中你可以执行下面的代码来检查参数是否为 1,然后将 div1 设置为可见,对 div2 执行相同的操作

<div id="div1" style="display: <%=(request.getParameter("div").equals("1") ? "block" : "none") %>;">
于 2013-01-24T07:39:36.767 回答