【问题标题】:Retain filtering option when paginating分页时保留过滤选项
【发布时间】:2014-01-11 22:01:21
【问题描述】:

我正在尝试向这个分页/过滤脚本添加一个新功能,但到目前为止,还没有成功。我希望,当您更改页面时,您从右上角选择的过滤器(带有“Ordonare dupa...”的过滤器)保持选中状态,而不是切换回第一个选项。

这是我的网站 - http://www.wheelsmarket.ro/cautare/jante/1

为了分页/过滤,我使用了这个功能:

$(document).ready(function()
{
    $('.sortare').on('change', function(){
    $.ajax({
        url: '/filtrare-jante.php',
        type: 'POST',
        data: {'selected' : $(this).val(), 'pagina_id' : <?php echo $_GET['pagina'];?>},
        success: function(data){
            console.log(data); // do something with what is returned
            $('#myTest').html(data);
            $('#queryInitial').html('null');
        }
    });
});

所有查询都在 #myTest div 中进行,并且当您更改该选择时,myTest div 会被更改而无需重新加载。问题是选择框超出了#myTest div,所以我怀疑我是否可以使用我拥有的那个功能。 例如:

<select class="sortare select" >
    <option value="1">cele mai noi</option>
    <option value="2">cele mai ieftine</option>
    <option value="3">cele mai scumpe</option>
</select>
<div id="myTest">
code
</div>

【问题讨论】:

    标签: javascript php jquery ajax pagination


    【解决方案1】:

    如果我理解正确,您需要类似:

    1) 将 id 添加到您的选项中:

    <select class="sortare select" >
        <option id="sel1" value="1">cele mai noi</option>
        <option id="sel2" value="2">cele mai ieftine</option>
        <option id="sel3" value="3">cele mai scumpe</option>
    </select>
    <div id="myTest">
    code
    </div>
    

    并将 tha attr() 更改为在 jquery 中选择:

    $(document).ready(function()
    {
        $('.sortare').on('change', function(){
        selValue = $(this).val(); //---> Store this value in a variable.
        $.ajax({
            url: '/filtrare-jante.php',
            type: 'POST',
            data: {'selected' : $(this).val(), 'pagina_id' : <?php echo $_GET['pagina'];?>},
            success: function(data){
                console.log(data); // do something with what is returned
                $('#myTest').html(data);
                $('#queryInitial').html('null');
                $('#sel'+selValue).attr("selected","selected"); //---> Use the variable we created to determine which option should be set to selected dynamically
            }
        });
    });
    

    【讨论】:

    • 首先,我认为该函数缺少一对括号,例如'});'。其次,它没有奏效。我自己添加了缺少的括号,但是在您更改页面后,它仍然没有保留您选择的值。
    猜你喜欢
    • 1970-01-01
    • 2016-12-09
    • 1970-01-01
    • 2013-09-23
    • 2015-10-27
    • 1970-01-01
    • 2021-08-19
    • 2019-12-16
    • 1970-01-01
    相关资源
    最近更新 更多