0

我有像这样创建的小表格和链接

<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"

4

5 回答 5

0

You can change the form action using attr() method of jquery, For example

$('#link1').click(function(){
   $('#formId').attr('action', 'page1').submit();
});


$('#link2').click(function(){
   $('#formId').attr('action', 'page2').submit();
});
于 2013-10-07T04:29:40.763 回答
0

Here you go...

<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>
于 2013-10-07T04:29:56.370 回答
0

I have done some modification in your code. please check and let me know.

<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>
于 2013-10-07T04:46:52.220 回答
0

试试看,

HTML的变化

<a href="#" id="msgSubmit">Message</a>

脚本

$('#msgSubmit').on('click',function(e){
   e.preventDefault();   
   $('form[name="selectform"]').attr('action','edit.php')
                               .removeAttr('target')
                               .submit();   
});
于 2013-10-07T04:30:22.920 回答
0

您可以使用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> 
于 2013-10-07T04:31:42.377 回答