JavaScript中的form对象

JavaScript中的form对象常用的方法与属性有submit(),reset(),focus(),value,action,readOnly,disabled.
submit()用于提交表单,reset()用于重置表单,focus()用于获取焦点。
value用于获取值和设置默认值,action用来设置表单的传送地址,readOnly设置只读,disabled设置禁用。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <form >
            <input name="wd" />
            <input type="button" value="百度一下" onclick="test2()"/>
        </form>
        <script>
            var form=document.getElementsByTagName("form")[0];/*document.getElementsByTagName("form")返回值是数组,这里我们只用数组的首元素,
            因为这个程序中就只有这一个form。当有多个时要注意要操作的是哪个form,注意要与其在数组中的下标对应*/
            form.action="http://www.baidu.com/s";//给form表单设置action
            function test()
            {
                form.submit();//作用跟type="submit"一样,一执行这个方法就会提交表单
            }

            var text=document.getElementsByName("wd")[0];
            text.focus();//实现一打开页面就在text框内自动获取焦点

            function test2()
            {
                console.log(text.value);//获取输入的值并输出
                text.readOnly=true;//设置只读;
                text.value="2233";//设置默认值,直接会在框内显示

                //text.disabled=true;//设置禁用;
            }//type为text,password,textarea均可用上面的属性
        </script>
    </body>
</html>

猜你喜欢

转载自blog.csdn.net/naruhina/article/details/81515316