zoukankan      html  css  js  c++  java
  • form 表单提交

    form 表单提交 4 种方式

    1、使用submit按钮提交表单

    
    <!DOCTYPE html>
    <html>
    <head lang="en">
        <meta charset="UTF-8">
        <title>form表单提交方式</title>
    </head>
    <body>
    <form action="demo_form.asp" method="post">
        First name:<br>
        <input type="text" name="firstname" value="Mickey"><br>
        Last name:<br>
        <input type="text" name="lastname" value="Mouse"><br><br>
        <input type="submit" value="提交">
    </form>
    <script>
        /*这里是提交表单前的非空校验*/
        $("form").submit(function () {
            var first = $("input[name='firstname']").val();
            var last = $("input[name='lastname']").val();
    
            if (first == "" || first == null || first == undefined) {
                alert("first");
                return false;/*阻止表单提交*/
            } else if (last == "" || last == null || last == undefined) {
                alert("last");
                return false;/*阻止表单提交*/
            } else {
                alert("提交");
                return true;
            }
        })
    </script>
    
    
    </body>
    </html>
    
    

    2、2.使用button按钮提交表单

    (1)可以直接将 上述方法1中的直接换成

    <!DOCTYPE html>
    <html>
    <head lang="en">
        <meta charset="UTF-8">
        <title>form表单提交方式</title>
    </head>
    <body>
    <form id="Form" action="" method="post" >
        <!--直接提交-->
        <input type="text" name="dingdanhao"><input id="chaxun" type="submit" value="查询"><br>
        First name:<br>
        <input type="text" name="firstname"><br>
        Last name:<br>
        <input type="text" name="lastname"><br><br>
        <input id="tj" type="button" value="提交" onclick="checkForm();">
    </form>
    <script src="jquery.js"></script>
    <script>
        /*这里是提交表单前的非空校验*/
        function checkForm () {
            var first = $("input[name='firstname']").val();
            var last = $("input[name='lastname']").val();
    
            if (first == "" || first == null || first == undefined) {
                alert("first");
                return false;/*阻止表单提交*/
            } else if (last == "" || last == null || last == undefined) {
                alert("last");
                return false;
            } else {
                alert("提交")
                $("#Form").submit();
            }
    
        }
    </script>
    </body>
    </html>
    
    

    3 、利用js进行表单提交,将form表单进行标记,将form表单中的某个元素设置点击事件,点击时调用js函数,再用js:如 $("#id").submit();等方法提交表单。

    <!DOCTYPE html>
    <html>
    <head lang="en">
        <meta charset="UTF-8">
        <title>form表单提交方式</title>
    </head>
    <body>
    <form id="Form" action="" method="post" onsubmit="return checkForm();">
        <!--直接提交-->
        <input id="first" type="text" name="dingdanhao"><input id="chaxun" type="submit" value="查询"><br>
        First name:<br>
        <input id="last" type="text" name="firstname"><br>
        Last name:<br>
        <input type="text" name="lastname"><br><br>
        <button type="button" onclick="subForm();">js提交</button>
    </form>
    <script src="jquery.js"></script>
    <script>
        /*这里是提交表单前的非空校验*/
        function checkForm () {
            var first = $("input[name='firstname']").val();
            var last = $("input[name='lastname']").val();
    
            if (first == "" || first == null || first == undefined) {
                alert("first不能为空");
                return false;/*阻止表单提交*/
            } else if (last == "" || last == null || last == undefined) {
                alert("last不能为空");
                return false;
            } else {
                alert("提交")
                return true;
            }
    
        }
        function subForm(){
            $("#Form").submit();
        }
    </script>
    </body>
    </html>
    
    
    

    4、 图片提交表单,将input的属性设置为image时,点击图片也可触发form表单的提交

    
    <!DOCTYPE html>
    <html>
    <head lang="en">
        <meta charset="UTF-8">
        <title>form表单提交方式</title>
    </head>
    <body>
    <form id="Form" action="" method="post" onsubmit="return checkForm();">
        <!--直接提交-->
        <input id="first" type="text" name="dingdanhao"><input id="chaxun" type="submit" value="查询"><br>
        First name:<br>
        <input id="last" type="text" name="firstname"><br>
        Last name:<br>
        <input type="text" name="lastname"><br><br>
        <input type="image" src="btn.png" style=" 50px;height: 50px">
    </form>
    <script src="jquery.js"></script>
    <script>
        /*这里是提交表单前的非空校验*/
        function checkForm() {
            var first = $("input[name='firstname']").val();
            var last = $("input[name='lastname']").val();
    
            if (first == "" || first == null || first == undefined) {
                alert("first不能为空");
                return false;
                /*阻止表单提交*/
            } else if (last == "" || last == null || last == undefined) {
                alert("last不能为空");
                return false;
            } else {
                alert("提交")
                return true;
            }
        }
    
    </script>
    </body>
    </html>
    
    
  • 相关阅读:
    Java路径问题终于解决方式—可定位全部资源的相对路径寻址
    易学设计模式看书笔记(2)
    js算法:分治法-棋盘覆盖
    [NIO]dawn之Task具体解释
    C#高级编程五十八天----并行集合
    [Swift通天遁地]九、拔剑吧-(12)创建Preview-Transition图像预览界面
    [Swift通天遁地]九、拔剑吧-(11)创建强大的Pinterest风格的瀑布流界面
    [Swift通天遁地]九、拔剑吧-(10)快速创建美观的聊天界面:可发送文字、表情、图片
    [Swift通天遁地]九、拔剑吧-(9)创建支持缩放、移动、裁切的相机视图控制器
    [Swift通天遁地]九、拔剑吧-(8)创建气泡式页面切换效果
  • 原文地址:https://www.cnblogs.com/gloria-liu/p/9087487.html
Copyright © 2011-2022 走看看