一个form表单有多个提交按钮

思路是这样的:先不指定表单的action,让action属性为"",method=“post”,为你的提交按钮全部替换为普通按钮,给它们都加上onclick事件,注意这里每一个按钮的onclick方法是一样的,只是传递的参数不一样,这里的参数只是起到标记是哪一个按钮的用。

最后根据onclick里的方法传递的参数不同,可以判断是来自哪一个按钮的消息,再为表单设置方法并提交。

这里有个案例(发布文章和将文章保存到草稿箱):

<form id="myFrm" action="" method="post">
文章标题:<input name="articleTitile"/><br/>
文章内容:<textarea name="articleContent"><br/>
<input type="button" value="发布文章" onclick="test(1)"/><br/>
<input type="button value="保存到草稿箱" onclick="test(2)"/> 
</form>
<script type="text/javascript">
function test(val){
    //判断参数
    if(val==1){  //发布文章的方法
        var url = "去到发布文章的Action";
        var params = $("#myFrm").serialize();
        //异步处理
        $.post(url,params);
    }
    if(val==2){  //保存到草稿箱
        var url = "去到草稿箱的Action";
        var params = $("#myFrm").serialize();
        //异步处理
        $.post(url,params);
    }
}
</script>

作者:hjl815
来源:CSDN
原文:https://blog.csdn.net/hjl815/article/details/77855694

原文:https://blog.csdn.net/xieyunc/article/details/80493366

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>支付测试模板</title>
</head>
<body>
<form action="" name="pay" method="post" enctype="multipart/form-data">
    评估类型:<input type="text" name="assess_type"/><br/>
    购买年限:<input type="text" name="assess_year"/><br/>
    邀请码数量:<input type="text" name="code_num"/><br/>
    购买年限:<input type="text" name="report_year"/><br/>
    是否开发票:<input type="text" name="report_bill"/><br/>
    姓名:<input type="text" name="report_address_uname"/><br/>
    电话:<input type="text" name="report_address_mobile"/><br/>
    地址:<input type="text" name="report_address"/><br/>
    单价:<input type="text" name="price"/><br/>
    数量:<input type="text" name="num"/><br/>
    总金额:<input type="text" name="total_fee"/><br/>
    <button onclick="wei()"/>微信支付</button>
    <button onclick="zhi()"/>支付宝支付</button>
</form>
</body>
</html>
 
<script>
    function wei(){
//        document.("表单的name值").action
//        document.("表单的name值").submit
        document.pay.action="{:U('Home/Payment/getQrcode')}";
        document.pay.submit();
    }
    function zhi() {
        document.pay.action = "{:U('Home/Payment/doalipay')}";
        document.pay.submit();
    }
</script>

或者:

<script language="javascript">
function save(){ 
    document.form1.action="right.asp";
    document.form1.submit();
}
function send(){
    document.form1.action="sendtaskook.asp";
    document.form1.submit();
}
</script>
<form name="form1">
<input type="button" name="btn1" value="发送" onclick="send();">
<input type="button" name="btn2" value="保存" onclick="save();">
</form>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值