[JavaScript基礎]學習③③--操作表單

HTML表單的輸入控件

文本框昆箕,對應的<input type="text">租冠,用于輸入文本;

口令框顽爹,對應的<input type="password">,用于輸入口令捏题;

單選框肉渴,對應的<input type="radio">,用于選擇一項循狰;

復選框券勺,對應的<input type="checkbox">,用于選擇多項昧识;

下拉框盗扒,對應的<select>缀去,用于選擇一項甸祭;

隱藏文本,對應的<input type="hidden">池户,用戶不可見,但表單提交時會把隱藏文本發(fā)送到服務器赊抖。

獲取值

// <input type="text" id="email">
var input = document.getElementById('email');
input.value; // '用戶輸入的值'

單選框和復選框

// <label><input type="radio" name="weekday" id="monday" value="1"> Monday</label>
// <label><input type="radio" name="weekday" id="tuesday" value="2"> Tuesday</label>
var mon = document.getElementById('monday');
var tue = document.getElementById('tuesday');
mon.value; // '1'
tue.value; // '2'
mon.checked; // true或者false
tue.checked; // true或者false

設置值

// <input type="text" id="email">
var input = document.getElementById('email');
input.value = 'test@example.com'; // 文本框的內(nèi)容已更新

HTML5控件

常用的包括date氛雪、datetime耸成、datetime-local、color等

<input type="date" value="2015-07-01">
<input type="datetime-local" value="2015-07-01T02:03:04">
<input type="color" value="#ff0000">

提交表單

submit()方法

<!-- HTML -->
<form id="test-form">
    <input type="text" name="test">
    <button type="button" onclick="doSubmitForm()">Submit</button>
</form>

<script>
function doSubmitForm() {
    var form = document.getElementById('test-form');
    // 可以在此修改form的input...
    // 提交form:
    form.submit();
}
</script>

響應<form>本身的onsubmit事件

<!-- HTML -->
<form id="test-form" onsubmit="return checkForm()">
    <input type="text" name="test">
    <button type="submit">Submit</button>
</form>

<script>
function checkForm() {
    var form = document.getElementById('test-form');
    // 可以在此修改form的input...
    // 繼續(xù)下一步:
    return true;
}
</script>

這個做法看上去沒啥問題弦追,但用戶輸入了口令提交時花竞,口令框的顯示會突然從幾個變成32個(因為MD5有32個字符)。

不改變用戶的輸入寇仓,可以利用<input type="hidden">

<!-- HTML -->
<form id="login-form" method="post" onsubmit="return checkForm()">
    <input type="text" id="username" name="username">
    <input type="password" id="input-password">
    <input type="hidden" id="md5-password" name="password">
    <button type="submit">Submit</button>
</form>

<script>
function checkForm() {
    var input_pwd = document.getElementById('input-password');
    var md5_pwd = document.getElementById('md5-password');
    // 把用戶輸入的明文變?yōu)镸D5:
    md5_pwd.value = toMD5(input_pwd.value);
    // 繼續(xù)下一步:
    return true;
}
</script>

沒有name屬性的<input>的數(shù)據(jù)不會被提交

練習

用戶名必須是3-10位英文字母或數(shù)字烤宙;

口令必須是6-20位俭嘁;

兩次輸入口令必須一致。

<!-- HTML結構 -->
<form id="test-register" action="#" target="_blank" onsubmit="return checkRegisterForm()">
    <p id="test-error" style="color:red"></p>
    <p>
        用戶名: <input type="text" id="username" name="username">
    </p>
    <p>
        口令: <input type="password" id="password" name="password">
    </p>
    <p>
        重復口令: <input type="password" id="password-2">
    </p>
    <p>
        <button type="submit">提交</button> <button type="reset">重置</button>
    </p>
</form>
'use strict';
var checkRegisterForm = function () {
            var username = document.getElementById('username');
            var password = document.getElementById('password');
            var passwd_2 = document.getElementById('password-2');
            var re = /[0-9a-zA-Z]*/;
            
            if (username.value.length > 10 || username.value.length < 3) {
                alert("false1");
                return false;
            } else if (!re.test(username)) {
                alert("false2");
                return false;
            } else if (password.value.length > 20 || password.value.length < 6) {
                alert("false3");
                return false;
            } else if (password.value != passwd_2.value) {
                alert("false4");
                return false;
            } else {
                alert("true");
                return true;
            }
        }

// 測試:
;(function () {
    window.testFormHandler = checkRegisterForm;
    var form = document.getElementById('test-register');
    if (form.dispatchEvent) {
        var event = new Event('submit', {
            bubbles: true,
            cancelable: true
          });
        form.dispatchEvent(event);
    } else {
        form.fireEvent('onsubmit');
    }
})();
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末近她,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子粘捎,更是在濱河造成了極大的恐慌危彩,老刑警劉巖泳桦,帶你破解...
    沈念sama閱讀 217,542評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件灸撰,死亡現(xiàn)場離奇詭異,居然都是意外死亡浮毯,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評論 3 394
  • 文/潘曉璐 我一進店門趋惨,熙熙樓的掌柜王于貴愁眉苦臉地迎上來惦蚊,“玉大人,你說我怎么就攤上這事兆沙±虻啵” “怎么了?”我有些...
    開封第一講書人閱讀 163,912評論 0 354
  • 文/不壞的土叔 我叫張陵库正,是天一觀的道長厘唾。 經(jīng)常有香客問我,道長抚垃,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,449評論 1 293
  • 正文 為了忘掉前任铣焊,我火速辦了婚禮罕伯,結果婚禮上乘陪,老公的妹妹穿的比我還像新娘。我一直安慰自己婿屹,他們只是感情好,可當我...
    茶點故事閱讀 67,500評論 6 392
  • 文/花漫 我一把揭開白布届腐。 她就那樣靜靜地躺著蜂奸,像睡著了一般。 火紅的嫁衣襯著肌膚如雪围详。 梳的紋絲不亂的頭發(fā)上祖屏,一...
    開封第一講書人閱讀 51,370評論 1 302
  • 那天,我揣著相機與錄音雹食,去河邊找鬼期丰。 笑死,一個胖子當著我的面吹牛钝荡,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播赎离,決...
    沈念sama閱讀 40,193評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼蟹瘾,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起狸捕,我...
    開封第一講書人閱讀 39,074評論 0 276
  • 序言:老撾萬榮一對情侶失蹤灸拍,失蹤者是張志新(化名)和其女友劉穎砾省,沒想到半個月后混槐,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,505評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡狠鸳,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,722評論 3 335
  • 正文 我和宋清朗相戀三年悯嗓,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片铅祸。...
    茶點故事閱讀 39,841評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡合武,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出盟庞,到底是詐尸還是另有隱情岂贩,我是刑警寧澤,帶...
    沈念sama閱讀 35,569評論 5 345
  • 正文 年R本政府宣布卸伞,位于F島的核電站锉屈,受9級特大地震影響,放射性物質發(fā)生泄漏颈渊。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,168評論 3 328
  • 文/蒙蒙 一雾家、第九天 我趴在偏房一處隱蔽的房頂上張望绍豁。 院中可真熱鬧,春花似錦、人聲如沸邪铲。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,783評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽英染。三九已至,卻和暖如春永丝,著一層夾襖步出監(jiān)牢的瞬間箭养,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,918評論 1 269
  • 我被黑心中介騙來泰國打工喝检, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留撼泛,地道東北人挠说。 一個月前我還...
    沈念sama閱讀 47,962評論 2 370
  • 正文 我出身青樓损俭,卻偏偏與公主長得像潘酗,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子仔夺,可洞房花燭夜當晚...
    茶點故事閱讀 44,781評論 2 354

推薦閱讀更多精彩內(nèi)容