1

データを送信しようとしているリモートサーバーにフォームがあります。例は次のとおりです。

<html>
<head>
</head>
<form  action="http://www.example.com/post.php"  method="post">
<input type="text" name="input1" value="test1"  />
<input type="text" name="input2" value="test2"  />
<input type="submit" name="send" value="Submit" />
</form>
</body>
</html>

このようなデータを自動送信しようとしています

<html>
<head>
</head>
<form  action="http://www.example.com/post.php"  method="post">
<input type="text" name="input1" value="test1"  />
<input type="text" name="input2" value="test2"  />
<script type="text/javascript"> 
 document.forms[0].submit(); 
</script> 
</form>
</body>
</html>

最初の例に(name = "send")名がない場合は正常に機能しますが、名前がある場合は何も送信されません。私の質問は、名前の付いた入力ボタンを使用してデータを送信するにはどうすればよいかということです。

ありがとうございました

4

2 に答える 2

2

Note: The answer turned out to be type="hidden" instead of type="submit", in which the second does not allow DOM submission while also submitting that input's value in the GET/POST data. type="hidden" does, and it works here since the button does not need to be physically clicked.


Pick one:

<form name="formname" id="formid" action="http://www.example.com/post.php" method="post">
<input type="text" name="input1" value="test1"  />
<input type="text" name="input2" value="test2"  />
<input type="submit" name="send" value="Submit" />
</form>
<script type="text/javascript">
if (confirm('Ok for "formname", Cancel for "getElementById"')) {
    console.log('document.formname.submit()');
    document.formname.submit();
} else {
    console.log('document.getElementById("formid").submit()');
    document.getElementById('formid').submit();
}
</script> 

http://jsfiddle.net/userdude/EAmwj/3

于 2012-08-15T00:18:33.930 に答える
2

ページが読み込まれるとすぐにJavaScriptコードが実行されます。そのため、すぐに送信されます。

JSコードを関数でラップし、イベントがページから呼び出すようにする必要があります。

通常のボタンを使用して、ボタンのonclickイベントの機能を設定できます。


<html>
<head>

<body>
<form  action="http://www.example.com/post.php"  method="post">
<input type="text" name="input1" value="test1"  />
<input type="text" name="input2" value="test2"  />
<input type="button" name="send" value="test2" onclick="doSubmit()"  />

<script type="text/javascript"> 
function doSubmit() {
 document.forms[0].submit(); 
}
</script> 
</form>
</body>
</html>
于 2012-08-15T00:42:20.937 に答える