当前位置: 移动技术网 > IT编程>开发语言>JavaScript > jQuery中Form相关知识汇总

jQuery中Form相关知识汇总

2019年03月30日  | 移动技术网IT编程  | 我要评论

form中的单行文本获取和失去焦点

代码如下:


<!doctype html>
<html>
<head lang="en">
    <meta charset="utf-8">
    <script type="text/javascript" src="../../js/jquery-2.1.3.js"></script>
    <title></title>
    <style type="text/css">
        input:focus, textarea:focus {
            border: 1px solid#f00;
            background: #fcc;
        }
        .focus {
            border: 1px solid#f00;
            background: #fcc;
        }
    </style>
</head>
<body>
<form action="#" method="post" id="regform">
    <fieldset>
        <legend>个人基本信息</legend>
        <p>
            <label for="username">名称:</label>
            <input id="username" type="text">
        </p>
        <p>
            <label for="pass">密码:</label>
            <input id="pass" type="password">
        </p>
        <p>
            <label for="msg">详细信息:</label>
            <textarea id="msg"></textarea>
        </p>
    </fieldset>
</form>
</body>
<script type="text/javascript">
    /**
     * 1.单行文本框---得到焦点和失去焦点
     * */
    $(function () {
        $(":input").focus(function () {
            $(this).addclass("focus");
        }).blur(function () {
            $(this).removeclass("focus");
        })
    })
</script>
</html>

 

更改多行文本的高度

 

代码如下:


<!doctype html>
<html>
<head lang="en">
    <meta charset="utf-8">
    <script type="text/javascript" src="../../js/jquery-2.1.3.js"></script>
    <title></title>
    <style type="text/css">
        * { margin:0; padding:0;font:normal 12px/17px arial; }
        .msg {width:300px; margin:100px; }
        .msg_caption { width:100%; overflow:hidden; margin-bottom:1px;}
        .msg_caption span { display:block; float:left; margin:0 2px; padding:4px 10px; background:#898989; cursor:pointer;color:white; }
        .msg textarea{ width:300px; height:80px;height:100px;border:1px solid #000;}
    </style>
</head>
<body>
<form>
    <p class="msg">
        <p class="msg_caption">
            <span class="bigger">放大</span>
            <span class="smaller">缩小</span>
        </p>
        <textarea id="comment" rows="8" cols="20">多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。
            多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。
        </textarea>
    </p>
</form>
</body>
<script type="text/javascript">
    /**
     * 多行文本框的高度调整
     * */
    $(function () {
        var $comment = $('#comment');
        $('.bigger').click(function () {
            if(!$comment.is(":animated")) {
         nbsp;       if($comment.height() < 500) {
                    //$comment.height($comment.height() + 50);//版本1
                    $comment.animate({height: "+=50"}, 400);
                }
            }
        });
        $('.smaller').click(function () {
            if(!$comment.is(":animated")) {
                if($comment.height() > 50) {
                    //$comment.height($comment.height() - 50);
                    $comment.animate({height: "-=50"}, 400);
                }
            }
        });
    });
</script>
</html>

 

更改多行文本的滚动条高度

 

代码如下:


<!doctype html>
<html>
<head lang="en">
    <meta charset="utf-8">
    <script type="text/javascript" src="../../js/jquery-2.1.3.js"></script>
    <title></title>
    <style type="text/css">
        * { margin:0; padding:0;font:normal 12px/17px arial; }
        .msg {width:300px; margin:100px; }
        .msg_caption { width:100%; overflow:hidden; margin-bottom:1px;}
        .msg_caption span { display:block; float:left; margin:0 2px; padding:4px 10px; background:#898989; cursor:pointer;color:white; }
        .msg textarea{ width:300px; height:80px;height:100px;border:1px solid #000;}
    </style>
</head>
<body>
<form>
    <p class="msg">
        <p class="msg_caption">
            <span class="up">向上</span>
            <span class="down">向下</span>
        </p>
        <textarea id="comment" rows="8" cols="20">多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。
            多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。
            多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。
            多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。
            多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。
            多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。
            多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。
            多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。
            多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。
        </textarea>
    </p>
</form>
</body>
<script type="text/javascript">
    /**
     * 多行文本框的滚动条高度调整
     * */
    $(function () {
        var $comment = $('#comment');
        $('.up').click(function () {
            if(!$comment.is(":animated")) {
                if($comment.height() < 500) {
                    $comment.animate({scrolltop: "+=50"}, 400);
                }
            }
        });
        $('.down').click(function () {
            if(!$comment.is(":animated")) {
                if($comment.height() > 50) {
                    $comment.animate({scrolltop: "-=50"}, 400);
                }
            }
        });
    });
</script>
</html>

 

复选框应用

 

代码如下:


<!doctype html>
<html>
<head lang="en">
    <meta charset="utf-8">
    <script type="text/javascript" src="../../js/jquery-2.1.3.js"></script>
    <title></title>
    <style type="text/css">
        input:focus, textarea:focus {
            border: 1px solid#f00;
            background: #fcc;
        }
        .focus {
            border: 1px solid#f00;
            background: #fcc;
        }
    </style>
</head>
<body>
<form>
    你爱好的运动是?<br/>
    <input type="checkbox" name="items" value="足球"/>足球
    <input type="checkbox" name="items" value="篮球"/>篮球
    <input type="checkbox" name="items" value="羽毛球"/>羽毛球
    <input type="checkbox" name="items" value="乒乓球"/>乒乓球
    <input type="button" id="checkedall" value="全  选"/>
    <input type="button" id="checkedno" value="全不选"/>
    <input type="button" id="checkedrev" value="反  选"/>
    <input type="button" id="send" value="提交"/>
</form>
</body>
<script type="text/javascript">
    /**
     * 复选框应用
     * */
    $(function () {
        $("#checkedall").click(function () {
            $('[name=items]:checkbox').attr('checked', true);
        });
        $("#checkedno").click(function () {
            $('[name=items]:checkbox').attr('checked', false);
        });
        $("#checkedrev").click(function () {
            $('[name=items]:checkbox').each(function () {
                this.checked = !this.checked;
            });
        });
        $("#send").click(function () {
            var str = "你选中的是: \r\n";
            $('[name=items]:checkbox:checked').each(function () {
                str += $(this).val() + "\r\n";
            });
            alert(str);
        });
    })
</script>
</html>

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网