简体   繁体   English

Ajax +浏览器后退按钮=复选框不起作用

[英]Ajax + browser backbutton = checkbox not working

I have a content page which is loaded via Ajax with a checkbox on it. 我有一个内容页面,该页面通过Ajax加载并带有一个复选框。 The checkboxes works as it should when I click the normal menu buttons on my webpage to get to the content. 当我单击网页上的常规菜单按钮以获取内容时,该复选框可以正常工作。 But when I go to another page and use the back button to get back to the page which has the checkboxes, the checkboxes stop working. 但是,当我转到另一个页面并使用“后退”按钮返回到具有复选框的页面时,这些复选框将停止工作。

html: HTML:

<div id="filters">
    <ul id="filters-background">
        <li id="option-type">Filter Options</li>
        <li class="wave2"><label for="white">White<input type="checkbox" name="white" value=".White" id="white"></label></li>
        <li class="wave2"><label for="blue">Blue<input type="checkbox" name="blue" value=".Blue" id="blue"></label></li>
    </ul>
</div>

<script type="text/javascript">
    OnSort();
</script>

js: JS:

function OnSort(e) {
    console.log('aaa');

    // filter button click
    var filterCheckboxes = $('#filters input');
    var filters = [];
    filterCheckboxes.change(function() {
        console.log('clicked checkbox');

        filterCheckboxes.filter(':checked').each(function() {
            console.log('working');
            filters.push( this.value );
        });
    });
}

If you look at the above code, the console will output 'aaa' when I click on the back button but if I test a checkbox it will not output 'clicked checkbox' in the console. 如果您看上面的代码,当我单击后退按钮时,控制台将输出“ aaa”,但是如果我测试复选框,则在控制台中将不会输出“单击的复选框”。 How can I make it so the checkboxes will keep working even after I use the browser back button? 如何做到这一点,即使我使用浏览器的“后退”按钮,复选框也能正常工作?

Maybe relevant PageLoad and Backbutton Ajax code: 也许相关的PageLoad和Backbutton Ajax代码:

function OnLoadPage(e) {
    e.preventDefault();
    var pageurl = $(this).data('url');
    $.ajax({
        type: 'GET',
        url: pageurl,
        success: function(data) {
            $('#content').html(data['content']); // update content

            if(data['menu']) { // update menu
                $('#menu').html(data['menu']);
            }
            else {
                $('#menu').html('');
            }

            // update url
            window.history.pushState({path:pageurl}, '', pageurl);
        },
        error: function(response) {
           alert('ERROR:' + response.responseText);
        }
    });
}

function InitOverrideBrowserBackButton() {
    // override the back button to get the ajax content without page reload
    $(window).bind('popstate', function() {
        $.ajax({url:location.pathname+'?rel=tab', success: function(data){
            $('#content').html(data['content']); // update content

            if(data['menu']) { // update menu
                $('#menu').html(data['menu']);
            }
            else {
                $('#menu').html('');
            }
        }});
    });
}

I've also tried: 我也尝试过:

$(document).on('click', '#filters input', function() {
    console.log('clicked checkbox');

    filterCheckboxes.filter(':checked').each(function() {
        console.log('working');
        filters.push( this.value );
    });
});

Which will output 'aaa' and 'clicked checkbox' but not 'working'. 它将输出“ aaa”和“单击的复选框”,但不输出“工作”。

Figured it out with help from others on StackOverflow. 在StackOverflow上的其他人的帮助下找到了答案。 Here's the answer: 答案如下:

function OnSort(e) {
    console.log('aaa');

    // filter button click
    var filters = [];
    $(document).on('click', '#filters input', function() {
        console.log('clicked checkbox');

        $('#filters input', document).filter(':checked').each(function() {
            console.log('working');
            filters.push( this.value );
        });
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM