2013-05-30 153 views
0

我有以下的jQuery的觸發事件的jQuery:在頁面加載

// change the number of finished and missing assets 
$('.add-requirements .overall-status .status-dropdown li').live('click', function() { 
    var remaining_titles = $('.item-section.finished').length; 
    $('.add-requirements .remaining-titles').text(remaining_titles); 
}); 

據中時將觸發狀態下拉菜單改變。當頁面加載時,我怎樣才能讓它啓動?

回答

1

從我能理解你想要更新在頁面加載時的剩餘計數,我假設finished狀態預先設置在所需的元素上。

我會去了解這個以另一種方式

$('.add-requirements .overall-status .status-dropdown li').live('click', updateRemaining); 
updateRemaining() 

function updateRemaining(){ 
    var remaining_titles = $('.item-section.finished').length; 
    $('.add-requirements .remaining-titles').text(remaining_titles); 
} 
1

trigger('click')剛剛鏈觸發第一頁面加載點擊:

$('.add-requirements .overall-status .status-dropdown li').live('click', function() { 
    var remaining_titles = $('.item-section.finished').length; 
    $('.add-requirements .remaining-titles').text(remaining_titles); 
}).trigger('click'); 

但你確實應該使用on()

$('closest_non_dynamic_parent').on('click', 'your_freakishly_long_selector', function() { 
+0

這似乎讓像5000倍的情況下火的時候我嘗試這種方式。 – David542

+0

@ David542 - 那麼你在做什麼嚴重的錯誤? – adeneo

+0

@adeneo這裏的問題是它會觸發每個'li'元素上的click事件,這可能不是OP所需要的,它看起來像OP想要更新剩餘計數 –

0

更詳細一點,但這裏是我工作:

var remaining_titles_section = $('.add-requirements .remaining-titles'); 
remaining_titles_section.text($('.item-section.finished').length); 
$('.add-requirements .overall-status .status-dropdown li').live('click', function() { 
    var remaining_titles = $('.item-section.finished').length; 
    remaining_titles_section.text(remaining_titles); 
});