中心window.open在表单提交 [英] center window.open on form submit

查看:98
本文介绍了中心window.open在表单提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如下所示:

<script type="text/javascript">
      function directions(sacred) {
        var x = screen.width / 2 - 700 / 2;
        var y = screen.height / 2 - 450 / 2;
        window.open(sacred.action, 'Directions', 'height=485,width=700,left=' + x + ',top=' + y);
        return false;
      }
</script>

<form action="http://maps.google.com/maps" method="get" target="Directions"
      onsubmit="return directions(sacred);">

我不懂js,所以如果看起来很草率,请放轻松。

I don't understand js, so take it easy on me if it looks sloppy.

我可以让它工作正常:

<form action="http://maps.google.com/maps" method="get" target="Directions"
    onsubmit="Directions=window.open('about:blank','Directions','width=600,height=400');">

一旦我尝试将 onsubmit 连接到上面的剧本,我迷路了。我甚至都不知道上述功能是否可靠。

Once I try to connect the onsubmit to the above script, I get lost. I don't even know if the above function is reliable.

我在这里打开引擎盖: jsFiddle

I have popped open the hood here: jsFiddle

问题是表单提交到新选项卡,并忽略 window.open 一起。

The problem there is the form submits into a new tab, and ignores window.open altogether.

提前感谢您的帮助。

推荐答案

试试这个: http://jsfiddle.net/333Qy/ 1 /

<script>
  function directions(sacred) {
  var x = screen.width / 2 - 700 / 2;
  var y = screen.height / 2 - 450 / 2;
  console.info(sacred);
  window.open(sacred.action, 'Directions', 'height=485,width=700,left=' + x + ',top=' + y);
  return false;
}
</script>
<p>
  <form action="http://maps.google.com/maps" method="get" target="Directions"
  onsubmit="directions(this);">
    <input class="directions-input" id="saddr" name="saddr" type="text" placeholder="enter zip-code"
    value="enter zip-code" onfocus="this.value = this.value=='enter zip-code'?'':this.value;"
    onblur="this.value = this.value==''?'enter zip-code':this.value;" />
    <input type="submit" class="directions-submit" />
    <input type="hidden" name="daddr" value="210+East+Northampton+Street,+Bath,+PA"
    />
    <input type="hidden" name="hl" value="en" />
  </form>
</p>

onsubmit应该是函数调用。此外,神圣是不确定的。我在上面的代码

onsubmit should be a function call. Also, sacred is undefined. I have made the changes in the above code

这篇关于中心window.open在表单提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆