javascript基础(四): 操作表单

表单是什么?form-----DOM树

  • 文本框----text
  • 下拉框----select
  • 单选框----radio
  • 多选框----checkbox
  • 隐藏域----hidden
  • 密码框----password

表单的目的提交信息

获得要提交的信息

<body>
    <form action = "post">
        <p>
            <span>用户名:</span><input type="text" id = "username" />
        </p>
        <!--多选框的值就是定义好的value-->
        <p>
            <span>性别:</span>
            <input type = "radio" name = "sex" value = "man" id = "boy"/>男
               <input type = "radio" name = "sex" value = "woman" id = "girl"/>女
        </p>
    </form>
    <script>
        var input_text = document.getElementById("username");
        var boy_radio = document.getElementById("boy");
        var girl_radio = document.getElementById("girl");
        //得到输入框的值
        input_text.value 
        //修改输入框的值
        input_text.value  = "value";
        
        //对于单选框,多选框等等固定的值,boy_radio.value只能取到当前的值
        boy_radio.checked;//查看返回的结果,是否为true,如果为true,则被选中。
        girl_radio.checked = true;//赋值
        
    </script>
</body>

 

提交表单。md5加密密码,表单优化

<!DOCTYPE html>
<html lang = "en">
    <head>
        <meta charset = "UTF-8">
        <title>Title</title>
        <!--MD5加密工具类-->
        <script src = "https://cdn.bootcss.com/blueimp-md5/2.10.0/js/md5.min.js">
            
        </script>
    </head>
    <body>
        <!--表达绑定提交事件
            οnsubmit= 绑定一个提交检测的函数,truefalse
            将这个结果返回给表单,使用onsubmit接收
        -->
        <form action = "https://www.baidu.com" method = "post" onsubmit = "return aaa()">
            <p>
                <span>用户名:</span><input type="text" id = "username" name = "username"/>
            </p>
            <p>
                <span>密码:</span><input type="password" id = "password" />
            </p>
            <input type = "hidden" id = "md5-password" name = "password"> 
            
            <!--绑定事件 onclick 被点击-->
            <button type = "submit">提交</button>
            
        </form>
        
        <script>
            function aaa(){
                alert(1);
                var username = document.getElementById("username");
                var pwd = document.getElementById("password");
                var md5pwd = document.getElementById("md5-password");
                //pwd.value = md5(pwd,value);
                md5pwd.value = mad5(pwd.value);
                //可以校验判断表单内容,true就是通过提交,false就是阻止提交
                return false;
                
            }
        </script>
        
    </body>
</html>

 

posted @ 2020-05-31 22:41  秋华  阅读(216)  评论(0编辑  收藏  举报