2013-09-29 57 views
0

當用戶開始在輸入字段中輸入內容時,我正在處理的Web應用程序會生成帶有自動填充選項的下拉列表,如果用戶單擊某個選項,它會填充相應的輸入字段與自動填充文本。模糊事件被觸發,而不是點擊

網絡應用程序包含許多發票行,每個發票行都有自己隱藏的自動填充下拉菜單,直到自動填充選項可用時纔會隱藏。

如果用戶點擊自動填充選項,我想使用此自動填充文本更新訂單。如果用戶沒有在自動填充選項上點擊並轉到下一個發票行,我仍然想用用戶輸入的純文本更新orderArray。

要做到這一點,我試圖用一個blur事件,無論是否被點擊下拉選項然而,這模糊事件觸發,我需要被觸發當且僅當來自其相應的下拉選項未被點擊

我明白爲什麼blur事件總是被觸發,但是我通過分離兩個事件並正確更新順序來解決這個問題非常困難。

我欣賞任何建議。

非常感謝提前!

$(".dropdown").on(click, ".item-option", function(){     
    //set item object with dropdown option text 
    ... 
    ... 
    ... 

    updateOrder(true, item); 
    $('.dropdown').hide(); 

}); 
//if customer enters custom information without clicking on item option 
$('.id-input, .name-input, .price-input, .qty-input').blur(function(){ 

     updateOrder(false, {}); 
}); 


function updateOrder(clicked, item){ 
    if(clicked==true){ 
     //update order with the item information from the dropdown the user clicked 
    }else{ 
     //update order by collecting all plaintext user entered into input fields 
    } 
} 
+1

您可以使用

$(".dropdown").on("mousedown", ".item-option", function(e){ e.stopPropagation(); //set item object with dropdown option text ... ... ... updateOrder(true, item); $('.dropdown').hide(); return false; }); //if customer enters custom information without clicking on item option $('.id-input, .name-input, .price-input, .qty-input').blur(function(){ updateOrder(false, {}); }); function updateOrder(clicked, item){ if(clicked==true){ //update order with the item information from the dropdown the user clicked }else{ //update order by collecting all plaintext user entered into input fields } } 

希望對大家有所幫助..乾杯:) .. 'event.preventDefault();' 或 'event.stopPropagation();' 防止事件冒泡。 –

+0

感謝您的回覆。你能否詳細說明一下? – AnchovyLegend

回答

2

好看一看這些變化,我在你的JS做了:我觀察到 :
click事件的模糊後觸發。
而不是點擊使用 mousedown它會工作。

這裏有我在你的JS所做的更改:

0

請嘗試以下代碼。

$(".dropdown").on(click, ".item-option", function(event){     
    //set item object with dropdown option text 
    ... 
    ... 
    ... 
    updateOrder(true, item); 
    $('.dropdown').hide(); 
    event.stopPropagation(); 
}); 

//if customer enters custom information without clicking on item option 
$('.id-input, .name-input, .price-input, .qty-input').blur(function(){ 
    updateOrder(false, {}); 
}); 


function updateOrder(clicked, item){ 
    if(clicked==true){ 
    //update order with the item information from the dropdown the user clicked 
    } else{ 
    //update order by collecting all plaintext user entered into input fields 
    } 
} 

stopPropagation只需要在點擊事件,因爲一旦下拉選項點擊,模糊也觸發,我們需要停止。模糊不觸發點擊,所以停止傳播不需要那裏。

+0

感謝您的建議,但是,使用這種方法,並沒有解決問題,「模糊」事件仍然始終觸發。我希望我可以保留'click'事件,因爲它更符合我需要的內容,但是'mousedown'結束了訣竅。 – AnchovyLegend