首页 > 解决方案 > 如何为 Ajax 数据分配默认值或预加载数据

问题描述

我正在使用复选框创建一个过滤器并且能够做到。但是,我的问题是如何在不向#response提交表单的情况下分配默认或预加载循环。执行下面的脚本将返回一个循环。我正在尝试学习 ajax,但在 PHP 中,逻辑类似于

    $response = 'myloop';

echo $response; // display the default loop

if ($search == 'true') {
    $response  = 'mynewloop'; // change the default loop
}
else {
    $response = 'myloop'; // return to default loop
}

这是我的ajax jquery

jQuery(function($){
    $('#filter').submit(function(){
        var filter = $('#filter');
        $.ajax({
            url:filter.attr('action'),
            data:filter.serialize(), // form data
            type:filter.attr('method'), // POST
            beforeSend:function(xhr){
                filter.find('button').text('Processing...'); // changing the button label
            },
            success:function(data){
                filter.find('button').text('Apply filter'); // changing the button label back
                $('#response').html(data); // insert data
            }
        });
        return false;
    });
});

标签: javascriptjqueryajaxform

解决方案


推荐阅读