[英]How to submit one html form to difference actions
我有小的表格和這樣創建的鏈接
<a href="#" onclick="document.selectform.submit();">Contact</a> | <a href="#">Message</a>
<form name="selectform" action="save.php" method="post" target="_blank">
Name: <input type="text" name="test"/>
<input type="submit" value="Submit" name="sub"/>
</form>
當我單擊“聯系人”鏈接時,表單正確提交,並且如果我單擊“消息”鏈接,則如何設置此表單提交到另一個URL
例如:如果我單擊“消息”鏈接,我想提交表單到edit.php,並且不使用traget =“ _ blank”
您可以使用jatt的attr()方法更改表單動作,例如
$('#link1').click(function(){
$('#formId').attr('action', 'page1').submit();
});
$('#link2').click(function(){
$('#formId').attr('action', 'page2').submit();
});
干得好...
<a href="#" onclick="document.selectform.submit();">Contact</a> | <a href="#" onclick="document.selectform.action='edit.php'; document.selectform.submit();">Message</a>
<form name="selectform" action="save.php" methode="post" target="_blank">
Name: <input type="text" name="test"/>
<input type="submit" value="Submit" name="sub"/>
</form>
嘗試一下,
HTML變更
<a href="#" id="msgSubmit">Message</a>
腳本
$('#msgSubmit').on('click',function(e){
e.preventDefault();
$('form[name="selectform"]').attr('action','edit.php')
.removeAttr('target')
.submit();
});
您可以使用onclick
事件
<script>
function submitForm(action)
{
document.getElementById('form1').action = action;
document.getElementById('form1').submit();
}
</script>
...
<a href="#" onclick="submitForm('page1.php')" value="submit 1">Contact</a>
<a href="#" onclick="submitForm('page2.php')" value="submit 2" >Message</a>
我已經對您的代碼進行了一些修改。 請檢查並讓我知道。
<a href="#" onclick="return myaction('save.php');">Contact</a> | <a href="#" onclick="return myaction('edit.php');">Message</a>
<form id="selectform_id" name="selectform" action="" method="post" target="_blank">
Name: <input type="text" name="test"/>
<input type="submit" value="Submit" name="sub"/>
</form>
<script type="text/javascript">
function myaction(actn){
document.getElementById("selectform_id").action = actn;
document.selectform.submit();
}
</script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.