我目前检查输入/文本区域/复选框/选择是否更改,如果是,那么我想启动我的函数来搜索数据库并更新“结果”div。现在这就是我到目前为止所拥有的..
它适用于更改没问题,唯一的问题是现在当我更改表单项时,它会检测到它,但新值不会传递给 .php 文件。建议?
注意:我尝试使用 $(this).serialize(); 但这对我不起作用。我只是想将所有指定的表单变量传递给 PHP 文件。
$(document).ready(function() { 
    $("select,:checkbox").change(function(){ inits(); });
    $("input,textarea").keystop(function(){ inits(); });
    a = "GET";
    b = "#recipeSearch";
    c = $(b).attr('action') + '?r';
    f = "#searchResults";
    function inits(){
        var values = {
            'term' : $("input#searchterm").val(),
            'photos' : $(b + " #requirephotos").val(),
            'prep' : $(b + " #preptime").val(),
            'cook' : $(b + " #cooktime").val()
        }
        console.log('Search Init');
        $.ajax({
            url: c,
            cache: false,
            dataType: 'json',
            data: values,  // $(b).serialize(); // this still didn't work
            success: function(response) {
                //decoded = JSON.stringify(response);
                if (response) {
                    $(f).html(response);
                }
                //$.each(r.items, function(i,data){} //usage later on 
            },
            error: function(e, jqxhr, settings, exception) {
                console.log('Error: ' + e.toString() + ' - ' + jqxhr);
            }
        });
    }
});
任何和所有的建议表示赞赏。我没有 jsfiddle 给你,因为它使用另一个脚本作为 keystop。
示例 JSON
{
"items": [
        "item 1",
        "item 2",
        "item 3",
        "item 4",
        "item 5"
        ]
}