0

我有以下 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);
});

每当更改状态下拉列表时,它就会触发。当页面加载时,我如何让它触发?

4

3 回答 3

1

只需链接 atrigger('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() {
于 2013-05-30T01:29:54.257 回答
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);
}
于 2013-05-30T04:06:01.173 回答
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);
});
于 2013-05-30T01:35:51.020 回答