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>
    
    
  • 相关阅读:
    人生,别认输,因为没人希望你赢
    一张图看懂开源许可协议,开源许可证GPL、BSD、MIT、Mozilla、Apache和LGPL的区别
    Android Studio 出现 Gradle's dependency cache may be corrupt 解决方案
    清华梦的粉碎——转自王垠
    label smooth
    <现代C++实战30讲>笔记 01 | 堆、栈、RAII:C++里该如何管理资源?
    h5转pb的两个坑
    opencv库的像素x,y,width,height,col,row的对应关系
    detect 导图
    keras多gpu训练
  • 原文地址:https://www.cnblogs.com/gloria-liu/p/9087487.html
Copyright © 2011-2022 走看看