[英]HTML Form submit data
我在遠程服務器上有一個表單,我正在嘗試提交數據,例如下面
<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”)名稱但它確實有一個名稱沒有提交,它工作正常。 我的問題是如何使用具有名稱的輸入按鈕發送數據。
謝謝
注意:答案結果是type="hidden"
而不是type="submit"
,其中第二個不允許DOM提交,同時還在GET / POST數據中提交input
的值。 type="hidden"
可以,並且它在這里工作,因為按鈕不需要物理點擊。
選一個:
<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>
您的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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.