1. 程式人生 > >jQuery-form表單事件

jQuery-form表單事件

<form action="#" method="get">
        <input type="text" name="user">
        <input type="password" name="pwd">
        <button type="submit">提交</button>
        <button type="reset">重置</button>
    </form>

1. form表單的提交事件

$('form').submit(function (event) {

            console.log('form表單提交了......')
            // 終止預設事件的傳遞
            event.preventDefault()
        })

2. form表單的重置事件

      $('button[type="reset"]').click(function (event) {
            result = confirm('你確定要清空資料嗎?')
            if(result == false){
                // 中斷預設事件
                event.preventDefault()
            }
        })

3. 當輸入框內容發生改變時, 輸入框失去焦點的時候, 觸發該事件

  $('input[name="user"]').change(function () {
            if ($(this).val().length < 6){
                $(this).css('border','2px solid red')
            }else{
                $(this).css('border','2px solid black')
            }
        })

4. blur( ) 無論輸入框內容是否更改, 只要失去焦點就會觸發事件

$('input[name="pwd"]').blur(function () {
            console.log('///////')
        })