jQuery 기초 지식--Form 기초(계속)

45750 단어 jquery
드롭다운 상자 적용
<!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; }

        div.centent {

            float:left;

            text-align: center;

            margin: 10px;

        }

        span {

            display:block;

            margin:2px 2px;

            padding:4px 10px;

            background:#898989;

            cursor:pointer;

            font-size:12px;

            color:white;

        }

    </style>

</head>

<body>



<div class="centent">

    <select multiple id="select1" style="width: 100px; height: 160px;">

        <option value="1">  1</option>

        <option value="2">  2</option>

        <option value="3">  3</option>

        <option value="4">  4</option>

        <option value="5">  5</option>

        <option value="6">  6</option>

        <option value="7">  7</option>

        <option value="8">  8</option>

    </select>

    <div>

        <span id="add">       &gt;&gt;</span>

        <span id="add_all">       &gt;&gt;</span>

    </div>

</div>



<div class="centent">

    <select multiple id="select2" style="width: 100px; height: 160px;">



    </select>

    <div>

        <span id="remove">&lt;&lt;       </span>

        <span id="remove_all">&lt;&lt;       </span>

    </div>

</div>



</body>



<script type="text/javascript">

    $(function () {



        $("#add").click(function () {

//            //  1

//            var $options = $('#select1 option:selected');

//            var $remove = $options.remove();

//            $remove.appendTo('#select2');

            //  2

            var $options = $('#select1 option:selected');

            $options.appendTo('#select2');

        });



        $("#add_all").click(function () {

            var $options = $("#select1 option");

            $options.appendTo("#select2");

        });



        $("#select1").dblclick(function () {

            var $options = $('#select1 option:selected');

            $options.appendTo('#select2');

        });



        $("#remove").click(function () {

            var $options = $("#select2 option:selected");

            $options.appendTo("#select1");

        });



        $("#remove_all").click(function () {

            var $options = $("#select2 option");

            $options.appendTo("#select1");

        });



        $("#select2").dblclick(function () {

            var $options = $("#select2 option:selected");

            $options.appendTo("#select1");

        });

    })

</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>

    <link href="css/style.css" rel="stylesheet" type="text/css" />

</head>

<body>



<form method="post" action="">

    <div class="int">

        <label for="username">   :</label>

        <input type="text" id="username" class="required" />

    </div>

    <div class="int">

        <label for="email">  :</label>

        <input type="text" id="email" class="required" />

    </div>

    <div class="int">

        <label for="personinfo">    :</label>

        <input type="text" id="personinfo" />

    </div>

    <div class="sub">

        <input type="submit" value="  " id="send" /><input type="reset" id="res" />

    </div>

</form>



</body>



<script type="text/javascript">

    $(function () {

        //      

        $("form :input.required").each(function () {

            $(this).parent().append("<strong class='high'>*</strong>");

        });



        //             

        $("form :input").blur(function () {

            var $parent = $(this).parent();

            $parent.find(".formtips").remove();

            //  username

            if($(this).is("#username")) {

                if(this.value == "" || this.value.length < 6) {

                    var errorMsg = "     6     .";

                    $parent.append("<span class='formtips onError'>" + errorMsg + "</span>")

                } else {

                    var okMsg = "    ";

                    $parent.append("<span class='formtips onSuccess'>" + okMsg + "</span>")

                }

            }



            //  email

            if($(this).is("#email")) {

                if(this.value == "" || (this.value != "" && !/.+@.+\.[a-zA-Z]{2,4}$/.test(this.value) ) ) {

                    var errorMsg = "        E-Mail  ";

                    $parent.append("<span class='formtips onError'>" + errorMsg + "</span>")

                } else {

                    var okMsg = "    ";

                    $parent.append("<span class='formtips onSuccess'>" + okMsg + "</span>")

                }

            }



        }).keyup(function () {//    

            $(this).triggerHandler("blur");

        }).focus(function () {

            $(this).triggerHandler("blur");

        });





        //  --        

        $("#send").click(function () {

            $("form .required:input").trigger("blur");

            var numErr = $("form .onError").length;

            if(numErr) {

                return false;

            }

            alert("    !");

        });

    })

</script>



</html>

좋은 웹페이지 즐겨찾기