【问题标题】:How to pass value to previous html page如何将值传递给上一个html页面
【发布时间】:2012-09-14 15:03:00
【问题描述】:

我有一个带有文本框的 HTML 页面。单击文本框时,会出现一个弹出窗口,单击“确定”按钮后,弹出窗口必须关闭,并且第一个 html 页面中的文本框必须填充第二个 HTML 中给出的值。 我试过这样的,

one.html

    <form name="form1" >
          <input type="text" name="source" onclick="window.open('second.html')" />
    </form>

second.html

  <input type="button" onclick="{document.form1.source.value='hello';window.close()}" />

【问题讨论】:

  • 我认为我们缺少一些代码...
  • @BvdVen:已编辑,换行符有问题:)
  • 从纯粹的设计角度来看,为什么不在同一个窗口中使用 JS 弹出窗口而不是打开一个新窗口(说实话有点烦人)?

标签: javascript html


【解决方案1】:

在你的 first.html 中

<form name="form1" >
         <input type="text" id="txt1" name="source" onclick="window.open('second.html')" />
    </form>
<script type="text/javascript" >
function setvalue(args)
  {
    document.getElementByid('txt1').value=args;
  }
</script>

second.html

  <input type="button" onclick="settoparent()" />
<script type="javascript">
  function settoparent()
   {
    if(window.opener.setvalue!=undefined)
     {
      setvalue("textboxvaluefromHTML2");
     }
   window.close()
   }
</script>

【讨论】:

    【解决方案2】:

    你必须使用

    window.opener.$("#yourtextfieldid").val(value);
    

    实现这一点

    在你的 vanila JS 中

    window.opener.document.yourelementID.fieldname.value="Any value";
    

    【讨论】:

    • 这意味着使用 JQuery,但显然他不是
    • @aravind.udayashankara:Javascript 不是 JQuery。 JQuery 是一个用 Javascript 编写的库。
    • @aravind.udayashankara:JQuery 不是什么神奇的东西,它是用 JS 写的,所以你用 jQuery 写的任何东西都可以用 JS 重写。例如$("#yourtextfield") 等于document.getElementById("yourtextfield")。顺便说一句,请注意,[烦人的]弹出窗口在任何人甚至想到编写 JQuery 之前就已经存在。
    • document.yourelementID.fieldname 应该只是 document.getElemenById("yourelementID") 什么是字段名?
    • @aravind.udayashankara 面对它,这不是一个 jQuery 问题。停止尝试战斗,cmets 是为建设性的 cmets 准备的。
    猜你喜欢
    • 2015-12-08
    • 2018-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多