2012-02-21 22 views
4

(對不起,我的英語)jQuery的變化集中在標籤或事件的內容

嗨!我使用jQuery中的應用,在那裏我有內像這樣的文本輸入的dinamycally創建的表:

<td><input type="text" id="code"></td> 
<td><select id="type"><option value="0">Normal</option><option value="1">Other</option></select></td> 
<td><input type="text" id="price" class="price"></td> 
<td><input type="text" id="total"></td> 

和其他部分我有一個按鈕,當點擊這個按鈕時,在表中創建另一條線。

此表的容器和按鈕存在模板中..這個模板使用underscore.js

現在的問題呈現:我需要以遍歷輸入:代碼,型號,價格。當我填寫輸入價格時,我計算總數並顯示在輸入中,然後我需要將焦點更改爲按鈕(#more_button)以讓用戶單擊或按Enter鍵在表中創建另一行。

我用這個:

$('.price').blur(function(e){ 
    _this.setTotal($(e.currentTarget).val()); 
    $('#more_button').removeClass('inactive').focus(); 

    e.preventDefault(); 
    e.stopPropagation(); 
}); 

當#more_button集中的CSS背景變化。

當我執行這段代碼時,按鈕會改變背景,但重點將更改爲url欄。這在Firefox和Chrome中發生。

我嘗試使用此設置對焦:

$('.price').blur(function(e){ 
    _this.setTotal($(e.currentTarget).val()); 
    $('#more_button').removeClass('inactive').; 
    setTiemout(function(){ 
     $('#more_button').focus(); 
    },100); 
    e.preventDefault(); 
    e.stopPropagation(); 
}); 

但不擦出火花.....

你能給一些指導,以acomplish呢?

用戶可以在按Tab或點擊頁面的其他部分時改變input.price的焦點..在這一刻我需要觸發seTotal並專注於按鈕。

回答

3

我不知道是什麼簡單的方法

$('your_selector').focusout(function(){ 
    $('#more_button').focus(); 
}); 

不TAB鍵工作(只用鼠標來改變焦點)..所以我利用keydown事件和聚焦之間的混合來解決問題。像這樣:

$('.price').bind('keydown',function(e){ 
    if(e.keyCode === 9){//Tab key 
    tab = true; 
    check(e); 
     return false; 
} 

}).bind('focusout',function(e){ 
if(!tab){ 
    check(e); 
}else{ 
    tab = false;  
} 
e.preventDefault(); 
return false; 
}); 

其中校驗()是驗證值,並且標籤是一個標誌,以檢查是否按下tab鍵的功能。

+0

謝謝,第二個選項效果很好。 – alsobubbly 2014-05-08 13:49:11

相關問題