不同页面传值的老版本
改进点:支持同页面有多个文本框,可自动向不同的文本框传值。先看演示吧

其实原理也很简单。同样先通过getAttribute判断type属性,捕捉到按钮。然后在按钮onclick时把i通过url传入子页面。

  1. <h2>input1</h2>
  2. <input type="text" /><input type="submit" />
  3. <h2>input2</h2>
  4. <input type="text" /><input type="submit" />
  5.  
  6. <script type="text/javascript"><!--
  7. var aInput = document.getElementsByTagName("input");
  8. for (i = 0 ; i < aInput.length ; i++ )
  9. {
  10.         (
  11.             function (i){
  12.                 if (aInput[i].getAttribute("type") == "submit")
  13.                 {
  14.                         aInput[i].onclick = function (){
  15.                         window.open('b.html?'+i,'newwindow','height=100,width=400')
  16.                     }
  17.                 }
  18.             }
  19.         )(i)
  20. }
  21. // --></script>



子页面用slice方法对url进行切分。并使用window.opener方法捕捉到父页面的文本框,进行赋值。 一切就OK了

  1. <h2>openWindow</h2>
  2. <input type="text" /><input type="submit" />
  3.  
  4. <script type="text/javascript"><!--
  5. var aInput = document.getElementsByTagName("input");
  6.  
  7. for (i = 0 ; i < aInput.length ; i++ )
  8. {
  9.     if (aInput[i].getAttribute("type") == "text") var textboxB = new Object(aInput[i]);
  10.     if (aInput[i].getAttribute("type") == "submit") var btnB = new Object(aInput[i]);
  11. }
  12.  
  13. btnB.onclick = function(){
  14.     var sTextValue = textboxB.value
  15.     var aInput = window.opener.document.getElementsByTagName("input");
  16.     var sUrl = document.location;
  17.     var sNo = sUrl.toString().slice(-1)
  18.     window.opener.aInput[sNo-"1"].value = sTextValue
  19.     window.close();
  20. }
  21. // --></script>

还没明白的朋友看一下原理图就知道了