-1

アップデート:

答えはここにあります:

中央のウィンドウ。フォーム送信時に開く

================================================== ====

画面の中央にあるポップアップにフォームを送信しようとしています。グーグルで見つけたいくつかのjsスニペットを「フランケンシュタイン」しました。

私は html/css は理解できますが、javascript は理解できません。

<html>

  <head>
    <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);
      }
    </script>
  </head>

  <body>
    <form action="http://maps.google.com/maps" method="get" target="Directions"
    onsubmit="Directions=directions(sacred)">
      <p>
        <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" />
      </p>
    </form>
  </body>

</html>

フィドルはこちら

フォームは新しいタブに送信され、関数または window.open は実行されません。

事前に誰にでも感謝します。

PS私はちょうどこのスタックオーバーフローのコツをつかんでいます。

4

2 に答える 2

0

フォームを送信しないようにするには、onsubmit関数で行う必要がありますreturn false

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;
  }

とフォーム:

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

変数sacredがどこから来ているのか正確にはわかりません。directions()また、現在の関数の戻り値をに格納しますDirectionsが、関数は現在何も返しません。しかし、私はあなたのコードからそれを理解することはできませんが、上記の例であなたはこれを機能させる方法のアイデアを得るはずです。

于 2013-01-07T23:42:37.223 に答える
0

あなたはそれをOKと書きましたが、迷子にならないように関数内で使用する方が常に良いです.

この機能を使用します:

function openCenteredWindow(url, name) {
    var width = 700;
    var height = 450;
    var left = parseInt((screen.availWidth/2) - (width/2));
    var top = parseInt((screen.availHeight/2) - (height/2));
    var windowFeatures = "width=" + width + ",height=" + height + ",status,resizable,left=" + left + ",top=" + top + "screenX=" + left + ",screenY=" + top;
    myWindow = window.open(url, name, windowFeatures);
}

違いがわかります。

于 2013-01-07T23:23:15.913 に答える