submit提交和button提交的区别

submit提交和button提交的区别

submit的提交:
1.一般submit提交之后就不再响应其他事件。
2.函数 onsubmit()可以用于验证表单提交,验证失败不能提交

下面是例子

 <!DOCTYPE html>
<html>
<head>
<title>login.html</title>
<script type="text/javascript">
    function testSubmit() {
        if (document.getElementById("login").value == "") {
            alert("用户名不能为空");
            return false;
        }else
        {
        document.getElementById("f1").action="/ajaxtest2/ajax/welcome.html";
            return true;

        }
    }
</script>


</head>

<body>
    <form name="f1" id="f1" action="" method="post" onsubmit="return testSubmit()">
        <table>
            <tr>
                <td>Login:</td>
                <td><input type="text" name="login" id="login">
                </td>
            </tr>
            <tr>
                <td>Password:</td>
                <td><input type="password" name="password" id="password">
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center"><input type="submit">
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

下面是button按钮的用法

<!DOCTYPE html>
<html>
<head>
<title>login.html</title>
<script type="text/javascript">
    function testSubmit() {
        if (document.getElementById("login2").value == "") {
            alert("用户名不能为空");
            document.getElementById("login2").focus();
            return false;
        }else
        {
        document.getElementById("f2").action="/ajaxtest2/ajax/welcome.html";
            return true;

        }
    }
</script>


</head>

<body>  
        <form name="f2" id="f2" action="" method="post">
        <table>
            <tr>
                <td>Login:</td>
                <td><input type="text" name="login2" id="login2">
                </td>
            </tr>
            <tr>
                <td>Password:</td>
                <td><input type="password" name="password2" id="password2">
                </td>
            </tr>
            <tr> 
                <td colspan="2" align="center"><input type="button" onclick="testSubmit()" value="提交">
                </td>
            </tr>
        </table>
    </form>
</body>
</html>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值