1. 程式人生 > >Jquery全面控制文字框技巧大全!(獲取值和賦值、清空等)

Jquery全面控制文字框技巧大全!(獲取值和賦值、清空等)

           $("#Text1").val("公交"); //賦值

           $("#Text1").val(""); //清空

          var mbt = $("#Text1").val();  // 獲取值

    $(”#Text1”)[0].focus()// 文字框獲得焦點

           $("#Text1").val("公交"); //賦值

           $("#Text1").val(""); //清空

          var mbt = $("#Text1").val();  // 獲取值

    $(”#Text1”)[0].focus()// 文字框獲得焦點

以下為轉================================

//當點選按鈕“新增空格”的時候,會在原來的內容的基礎上加上空格,但是這個時候文字框已經失去焦點了,需要通過focus()函式使其,在插入空格的位置上再次獲得游標。
$("#insert_blank").click(function()
{
var temp=$("#blank_qustion_content").val();
temp=temp+"______";
$("#blank_qustion_content").val(temp);
//插入完空格的時候,文字框再次獲得焦點
$("#blank_qustion_content").focus();
})

以下為轉================================

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="文字框失去和獲取焦點變色.aspx.cs" Inherits="文字框失去和獲取焦點變色" %>  
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
    <title></title>  
    <style type="text/css">  
        body  
        {  
            font: normal 12px/17px Arial;  
        }  
        div  
        {  
            padding: 2px;  
        }  
        input, textarea  
        {  
            width: 12em;  
            border: 1px solid #888;  
        }  
        .focus  
        {  
            border: 1px solid #f00;  
            background: #fcc;  
        }  
    </style>  
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>  
    <script type="text/javascript">  
        $(function () {  
            $("input").focus(function () {  
                $(this).addClass("focus");  
            }).blur(function () {  
                $(this).removeClass("focus");  
            });  
        });  
    </script>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <fieldset>  
        <legend>個人基本資訊</legend>  
        <div>  
            <label for="username">  
                名稱:</label>  
            <input id="username" type="text" />  
        </div>  
        <div>  
            <label for="pass">  
                密碼:</label>  
            <input id="pass" type="password" />  
        </div>  
        <div>  
            <label for="msg">  
                詳細資訊:</label>  
            <textarea id="msg" rows="2" cols="20"></textarea>  
        </div>  
    </fieldset>  
    </form>  
</body>  
</html>  

jQuery文字框中的事件應用

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>jquery文字框中的事件應用</title>
    <style type="text/css">
    body{ font-size:13px;}
    /*元素初始化樣式*/
    .divInit{ width:390px; height:55px; line-height:55px; padding-left:20px;}
    .txtInit{ border:solid 1px #666; padding:3px; background-image:url('Images/bg_email_input.gif');}
    .spnInit{ width:179px; height:40px; line-height:40px; float:right; margin-top:8px; padding-left:10px; background-repeat:no-repeat;}
    /*元素丟失焦點樣式*/
    .divBlur{ background-color:#FEEEC2;}
    .txtBlur{ border:solid 1px #666; padding:3px; background-image:url('Images/bg_email_input2.gif');}
    .spnBlur{ background-image:url('Images/bg_email_wrong.gif');}
    .divFocu{ background-color:#EDFFD5;} /*div獲取焦點樣式*/
    .spnSucc{ background-image:url('Images/pic_Email_ok.gif'); margin-top:20px;} /*驗證成功時span樣式*/
    </style>
    <script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $("#txtEmail").trigger("focus"); //預設時文字框獲得焦點
            $("#txtEmail").focus(function () { //文字框獲取焦點事件
                $(this).removeClass("txtBlur").addClass("txtInit");
                $("#email").removeClass("divBlur").addClass("divFocu");
                $("#spnTip").removeClass("spnBlur").removeClass("spnSucc").html("請輸入您常用郵箱地址!");
            });
            $("#txtEmail").blur(function () { //文字框丟失焦點事件
                var vTxt = $("#txtEmail").val();
                if (vTxt.length == 0) { //文字框中是否輸入了郵箱
                    $(this).removeClass("txtInit").addClass("txtBlur");
                    $("# email").removeClass("divFocu").addClass("divBlur");
                    $("#spnTip").addClass("spnBlur").html("郵箱地址不能為空!");
                }
                else { //檢測郵箱格式是否正確
                    if (!chkEmail(vTxt)) { //如果不正確
                        $(this).removeClass("txtInit").addClass("txtBlur");
                        $("#email").removeClass("divFocu").addClass("divBlur");
                        $("#spnTip").addClass("spnBlur").html("郵箱格式不正確!");
                    }
                    else { //如果正確
                        $(this).removeClass("txtBlur").addClass("txtInit");
                        $("#email").removeClass("divFocu");
                        $("#spnTip").removeClass("spnBlur").addClass("spnSucc").html("");
                    }
                }
            });
            /*驗證郵箱格式是否正確 引數strEmail,需要驗證的郵箱*/
            function chkEmail(strEmail) {
                var vChk = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
                if (!vChk.test(strEmail)) {
                    return false;
                }
                else {
                    return true;
                }
            }
        });
    </script>
</head>
<body>
<form id="form1" action="#">
    <div id="email" class="divInit">郵箱:
        <span id="spnTip" class="spnInit"></span>
        <input type="text" id="txtEmail" class="txtInit" />
    </div>
</form>
</body>
</html>