簡體   English   中英

如何使用java腳本將按鈕的值獲取到其他jsp中

[英]how to get the value of button into other jsp using java script

我必須在 jsp 的其他頁面上獲取按鈕值並根據條件執行<div>標記我的index.jsp代碼是這樣的

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

<html>
<head>
<script type="text/javascript">
   function popuponclick()
   {
      my_window = window.open("file.jsp",
       "mywindow","status=1,width=350,height=150");

 }



</script>
    </head>
    <table>
    <tr>
    <td><button type="button" onclick="popuponclick()" value="1" name="name">GetAllEmployeeByID</button></td>
    </tr>
    <tr>
    <td><button type="button" onclick="popuponclick()" value="2" name="name">GetEmployeeByName</button></td>
    </tr>
     </table>
    </body>
    </html>

我的second.jsp是這個

  <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt"%>
<%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql"%>
<%@ taglib prefix="x" uri="http://java.sun.com/jstl/xml"%>
<%@page import="com.nousinfo.tutorial.employee.service.model.bo.EmployeeBO" %>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    <script>
    function myfuntion(String name)
    { 
        location.
        document.getElementById("div1");
        }

</script>

</head>
<body>
    <form action="/EmployeeWeb/GetEmployee">
        <div id="div1" style="display: block;">
                <table>
                    <tr>
                        <td width="29">Find_Employee_By_Name:</td>
                        <td><input type="text" name="findbyname" /></td>


</tr>
                    <tr>
                        <td><input type="submit" name="submit" value="find"></td>
                    </tr>
                </table>
            </div>

 <div id="div2" style="display: none;">
                <table>
                    <tr>
                        <td width="29">Find_Employee_By_Name:</td>
                        <td><input type="text" name="findbyname" /></td>
                    </tr>
                    <tr>
                        <td><input type="submit" name="submit" value="find"></td>
                    </tr>
                </table>
            </div> 
    </form>
     <button onclick="myfunction()"></button>
</body>
</html>

我將如何在第二個 jsp 中獲取按鈕值,以便根據條件我可以運行<div>標記部分

在 index.jsp 頁面中試試這個:

function popuponclick()
{
    var my_window = window.open("file.jsp", "mywindow","status=1,width=350,height=150");

    // Gets the two divs
    var div1 = my_window.document.getElementById('div1');
    var div2 = my_window.document.getElementById('div2');

    // Hides the two divs
    div1.style.display = 'none';
    div2.style.display = 'none';

    // Check what the value of the button pressed and displays the correct div
    if(this.value = '1')
        div1.style.display = 'block';
    else
        div2.style.display = 'block';
}

如果我問對了你的問題,試試這樣的事情。 您需要向按鈕添加一些 ID 才能輕松選擇它們。

用按鈕 ID 替換(用斜杠)\\button id here\\,並將 \\some value\\ 替換為您想要匹配的值(例如 - 2)。

if (document.getElementById('\\button id here\\').value == \\some value){

} 別的 {

}

順便說一句,在第二個jsp中你得到了這個結構:location.document.getElementById

“位置”對象中沒有“文檔”屬性,請在沒有“位置”的情況下使用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM