0

document.writeが含まれている古いコードがあります。

<script type="text/javascript" language="JavaScript1.2">
document.write('<form name="InvGenPayDonation" action="'+PostURL+'" onsubmit="return validateForm();" method="POST">');
</script>

アクションを、ページが読み込まれたときに決定されたjavascript変数に置き換えるにはどうすればよいですか?これが関数の一部ではないそのコードです。(私は名前を置き換えたので、substrインデックスは正しくありませんが、あなたがポイントを得ると確信しています。)

// Check where we came from so that we can go to the right spot when the
// form gets posted from outside of our HTML tree.
var PostURL = '/event.php';

if (window.location.href.substr(0, 21) == 'http://10.10.10.10:80')
  PostURL = 'http://10.10.10.10:8580/event.php';
else if (window.location.href.substr(0, 14) == 'http://webapps')
  PostURL = 'http://10.100.0.11:8580/event.php';
else if (window.location.href.substr(0, 7) == 'webapps')
  PostURL = 'http://10.10.10.10:8580/event.php';
else if (window.location.href.substr(0, 10) == 'http://www')
  PostURL = 'https://secure.french.toast.new_zeland.france/event.php';

PostURLをフォームのアクションに入れてほしい。document.writeなしでそれができれば、それは素晴らしいことです。助けが必要です。ありがとう。

4

4 に答える 4

1

他のいくつかのDOM0コードでうまくいくはずです。

// Your code from the question here
document.forms[0].action = PostURL;

これは、フォームがページの最初のフォームであると想定していることに注意してください。そうでない場合は[0]、フォームのゼロベースのインデックスに置き換えてください。

ただし、ページのレイアウトに依存しない、より最新のソリューションを使用できます(おそらく使用する必要があります)。最も簡単な方法はdocument.getElementById、フォームにID属性を追加して(IDの値がページ全体で一意であることを確認してください)、次の手順を実行するだけです。

document.getElementById("yourFormID").action = PostURL;
于 2012-09-12T21:42:05.407 に答える
1

さて、あなたはhtmlであなたのフォームを作成し、それとidを与え、そしてそれをこのようにすることができます:

document.getElementById('myform').setAttribute('action', post_url);
于 2012-09-12T21:42:50.117 に答える
0

私はjqueryを使用してそれを行います。まだ知らない場合は、jqueryファイルも含める必要があります。

$(document).ready(function() {
var PostURL = '/event.php';

if (window.location.href.substr(0, 21) == 'http://10.10.10.10:80')
  PostURL = 'http://10.10.10.10:8580/event.php';
else if (window.location.href.substr(0, 14) == 'http://webapps')
  PostURL = 'http://10.100.0.11:8580/event.php';
else if (window.location.href.substr(0, 7) == 'webapps')
  PostURL = 'http://10.10.10.10:8580/event.php';
else if (window.location.href.substr(0, 10) == 'http://www')
  PostURL = 'https://secure.french.toast.new_zeland.france/event.php';


  $("#search-form").attr("action", PostURL);
});

<form action='' id="search-form">
</form>
于 2012-09-12T21:42:24.963 に答える
0

目的に応じてjQueryを使用できます。このコードは、document.writeなしでフォームのアクションを設定します。

$("form[name=InvGenPayDonation]").attr("action", PostURL);
于 2012-09-12T21:44:18.697 に答える