×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

Try my code here, there may be many ways to do it, but this one works as you expect.

本文发表在 rolia.net 枫下论坛parent.jsp

<head>
<script language="javascript">
function showPopup() {
window.open("popup.jsp"); //why not html, You must have a reason :)
}
</script>
</head>
<body>
<br>
<h3> Staff Information </h3>
<table border=1>
<tr><td colspan=2> &nbsp;</td></tr>
<tr>
<td>Name:&nbsp;<input type="text" size=20 name="staff_name" value="Xiao Xiao"></td>
<td>Id: &nbsp;<input type="text" size=20 name="staff_id" value="1"></td>
</tr>
<tr>
<td>Name:&nbsp;<input type="text" size=20 name="staff_name" value="Da Da"</td>
<td>Id: &nbsp;<input type="text" size=20 name="staff_id" value="2"></td>
</tr>
<tr><td colspan=2 align="center"><input type=button value="Popup" onclick="javascript:showPo
pup()"></td></tr>
</table>
</body>
</html>



popup.jsp

<html>
<head>
<title>Popup Window</title>
<script language=javascript>
function getData(){
var allStaffName = window.opener.document.getElementsByName("staff_name");
var allStaffId = window.opener.document.getElementsByName("staff_id");
for(i=0; i<allStaffName.length && i<allStaffId.length; i++)
document.write("<option value=" + allStaffId[i].value + ">" + allStaffName[i].value + "</opt
ion>");
}
</script>
</head>

<body>
<select name="staffs">
<option value=0>Staff List</option>
<script>javascript:getData()</script>
</select>
</body>

</html>更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / 请教:Javascript变量显示, thanks!
    var test='123';
    use alert to pop up a message '123' not 'test', how to write the script.
    it seems alert(test) doesn't work
    • It should be ok.
    • it works. 你怎么写的?
    • 换个变量名试试看, 不要用test, 比如用 a
    • Should I declare the variable in <head>??
      • Maybe my question is not clear. I want to use the variable in jsp.
        I don't want to use get or post method, just want to get parent window's fields' value through javascript variables, and use out.println() to display.
        Maybe it's not a good way, any suggestions?
        • 可以用hidden input来传递。
          • hidden field 肯定能做,但页面看来不整齐,debug也麻烦。
            • 她不想用post, get嘛。
              • 我就是想说这个东东不是好东东
            • I don't think so.
        • 你把client side script 和server side script搞混了。
          • I want to mix the clien/server side scripts together, should have some ways.
            • I don't think so. why not tell us what you want and we can find out a solution.
              • please see inside.Thanks.
                two jsp page: parent.jsp, popup.jsp
                in parent.jsp, use html link to open popup.jsp, don't want to use get method to pass parameters. For example, parent has staff name, staff id,
                popup.jsp has a dropdown list to display staff name with value of staff id.
                I want to get the value of parent's staff name and id using javascript
                variables: var staff_name = opener.document.staff_name.value; var staff_id = opener.document.staff_id.value, then using
                out.println("<option value=staff_id>staff_name</opotion>") to display the value in the dropdown list.
                my question is I cannot display staff_id and staff_name properly. I know something wrong with out.println() function, but how to fix it?
                Any suggestions are welcomed!
                • 就象#1130084说的,你把client side script 和server side script搞混了。staff_name是jscript中的变量,是在Client端运行得到的,而out.println是JSP中的语句,在服务器端运行。
                • out.println("<option value=staff_id>staff_name</opotion>")
                  shouldnt it be
                  out.println("<option value=" + staff_id + ">" + staff_name + "</opotion>")
    • Thanks, everyboday, good night!
    • Try my code here, there may be many ways to do it, but this one works as you expect.
      本文发表在 rolia.net 枫下论坛parent.jsp

      <head>
      <script language="javascript">
      function showPopup() {
      window.open("popup.jsp"); //why not html, You must have a reason :)
      }
      </script>
      </head>
      <body>
      <br>
      <h3> Staff Information </h3>
      <table border=1>
      <tr><td colspan=2> &nbsp;</td></tr>
      <tr>
      <td>Name:&nbsp;<input type="text" size=20 name="staff_name" value="Xiao Xiao"></td>
      <td>Id: &nbsp;<input type="text" size=20 name="staff_id" value="1"></td>
      </tr>
      <tr>
      <td>Name:&nbsp;<input type="text" size=20 name="staff_name" value="Da Da"</td>
      <td>Id: &nbsp;<input type="text" size=20 name="staff_id" value="2"></td>
      </tr>
      <tr><td colspan=2 align="center"><input type=button value="Popup" onclick="javascript:showPo
      pup()"></td></tr>
      </table>
      </body>
      </html>



      popup.jsp

      <html>
      <head>
      <title>Popup Window</title>
      <script language=javascript>
      function getData(){
      var allStaffName = window.opener.document.getElementsByName("staff_name");
      var allStaffId = window.opener.document.getElementsByName("staff_id");
      for(i=0; i<allStaffName.length && i<allStaffId.length; i++)
      document.write("<option value=" + allStaffId[i].value + ">" + allStaffName[i].value + "</opt
      ion>");
      }
      </script>
      </head>

      <body>
      <select name="staffs">
      <option value=0>Staff List</option>
      <script>javascript:getData()</script>
      </select>
      </body>

      </html>更多精彩文章及讨论,请光临枫下论坛 rolia.net
      • Thank you very much, it works! I didn't think about to use 'document.write'.
        • 'document.write' is a client side script, in that solution, it works, but no server side script used. so there is no reason to use a jsp file.
      • a simple htm file can do it , why need jsp?
        <html>
        <head>
        <title>Popup Window</title>
        </head>

        <body>
        <select name="staffs">
        <option value=0>Staff List</option>
        </select>
        <script language=javascript>
        var allStaffName = window.opener.document.getElementsByName("staff_name");
        var allStaffId = window.opener.document.getElementsByName("staff_id");
        var theStaffs = document.all.staffs.options;
        var newOption;
        var count;

        for(i=0; i<allStaffName.length && i<allStaffId.length; i++)
        {
        newOption = new Option();
        newOption.value = allStaffId[i].value
        newOption.text = allStaffName[i].value;
        count = theStaffs.length;
        theStaffs.add(newOption,count);
        }
        </script>
        </body>
        </html>
        • I think cloud2001 is devloping some web application, please see #1130178, what's shown here is just an example, you are right jsp is not needed for this example, but that's what the user asked. :)
          • your solution does not meet the requirement because no server side script is used to handle the data inputed at client side.