簡體   English   中英

輸入模糊后如何調用函數?

[英]How to call a function AFTER blur from input?

我正在使用jquery-ui datepicker作為輸入之一。 如果用戶因輸入內容模糊不清,則會顯示一條相關消息(例如“此字段為必填”)。 為此,我有以下jQuery,它對於簡單的<select><input type="text">標記非常適用:

$("input").blur(function(){
    if($(this).val()=="")
        $(this).next().text("This field is required.");
    else
        $(this).next().text("");
});

但是,在使用日期選擇器的情況下( 實際上必須 模糊選擇日期), 即使選擇了日期,錯誤消息也會顯示(即選擇日期會導致模糊事件)。 這要求.blur(function(){}); 完成后,模糊事件(非同時),應該被調用。

如何解決這種情況?

這是一個DEMO

您可以將驗證檢查添加到日期選擇器的onClose事件,而不是輸入字段的blur事件:

$("#datepicker").datepicker({onClose: function(selectedDate) {
    if(!selectedDate) {    
        $(this).next().text("This field is required.");
    }
}});

有點黑客,但您可以使用setTimeout延遲檢查:

$("input").blur(function () {
    var $t = $(this);
    setTimeout(function () {
        if ($t.val() == "") $t.next().text("This field is required.");
        else $t.next().text("");
    }, 100);
});

http://jsfiddle.net/D4AGz/96/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM