weixin_33726943 2015-09-04 04:35 采纳率: 0%
浏览 20

Ajax搜索页面加载

I have a ajax live search script working with php to search a database automatically when you type.

However I am now trying to use GET function to place a value into the search box from the url. Everything is working fine and the value is placed in to input box however it wont search unless you type.

How do i get it to search when the page loads with the value from the url?

Here is the ajax script:

var ls = {
    url: "ajax/process_livesearch.php",
    form_id: "#ls_form",
    form_anti_bot_id: "#ls_anti_bot",
    form_anti_bot: "Ehsan's guard",
    query_id: "#ls_query",
    result_id: "#ls_result_div",
    footer_id: "#ls_result_footer",
    current_page_hidden_id: "#ls_current_page",
    current_page_lbl_id: "#ls_current_page_lbl",
    last_page_lbl_id: "#ls_last_page_lbl",
    page_range_id: "#ls_items_per_page",
    navigation_class: ".navigation",
    arrow_class: ".arrow",
    next_page_id: "ls_next_page",
    previous_page_id: "ls_previous_page",
    slide_speed: "fast",
    type_delay: 350,
    select_column_index: 1
};
var result = $(ls.result_id);
var query = $(ls.query_id);
var footer = $(ls.footer_id);
var current_page = $(ls.current_page_hidden_id);
var current_page_lbl = $(ls.current_page_lbl_id);
var total_page_lbl = $(ls.last_page_lbl_id);
var page_range = $(ls.page_range_id);
var select_result;
function show_result() {
    result.slideDown(ls.slide_speed)
}
function hide_result() {
    result.slideUp(ls.slide_speed)
}
function remove_select_options(a) {
    var b, c;
    b = page_range.data("selected_option", page_range.val()).find("option");
    if (page_range.data("all_options") === undefined) {
        page_range.data("all_options", b)
    } else {
        page_range.empty();
        page_range.append(page_range.data("all_options"))
    }
    c = false;
    $(page_range.data("all_options")).each(function () {
        if (this.value >= a) {
            if (this.value === page_range.data("selected_option")) {
                c = true
            }
            $(this).remove()
        }
    });
    if (c) {
        page_range.val("0")
    } else {
        page_range.val(page_range.data("selected_option"))
    }
    if (page_range.find("option").length <= 1) {
        footer.hide();
        result.find("table").addClass("border_radius")
    } else {
        result.find("table").removeClass("border_radius");
        footer.show()
    }
}
function remove_footer() {
    result.off("click", "tr", select_result);
    footer.hide();
    result.find("table").addClass("border_radius")
}
function search_query(c, b, a) {
    if ($.trim(c.value).length) {
        if (b || c.latest_value !== c.value) {
            if (a) {
                current_page.val("1");
                current_page_lbl.html("1")
            }
            c.selected_row = undefined;
            if (c.to_be_executed) {
                clearTimeout(c.to_be_executed)
            }
            c.to_be_executed = setTimeout(function () {
                if ($.trim(query.val()).length) {
                    query.addClass("ajax_loader");
                    $.ajax({
                        type: "post",
                        url: ls.url,
                        data: $(ls.form_id).serialize(),
                        dataType: "json",
                        success: function (d) {
                            if (d.status === "success") {
                                var e = $.parseJSON(d.result);
                                result.find("table tbody").html(e.html);
                                if (e.number_of_results === 0) {
                                    remove_footer()
                                } else {
                                    if (e.total_pages > 1) {
                                        $(ls.navigation_class).show();
                                        total_page_lbl.html(e.total_pages)
                                    } else {
                                        $(ls.navigation_class).hide()
                                    }
                                    remove_select_options(e.number_of_results);
                                    result.on("click", "tr", select_result)
                                }
                            } else {
                                result.find("table tbody").html(d.message);
                                remove_footer()
                            }
                        },
                        error: function () {
                            result.find("table tbody").html("Something went wront. Please refresh the page.");
                            remove_footer()
                        },
                        complete: function () {
                            if ($.trim(c.value).length && result.is(":hidden")) {
                                show_result()
                            }
                            query.removeClass("ajax_loader")
                        }
                    })
                }
            }, ls.type_delay)
        }
    } else {
        if (result.is(":visible") || result.is(":animated")) {
            hide_result()
        }
    }
    c.latest_value = c.value
}
select_result = function () {
    query.val($(query.selected_row).find("td").eq(ls.select_column_index).html());
    hide_result()
};
function adjust_result_position() {
    $(result).css({left: query.position().left + 1, width: query.outerWidth() - 2})
}
$(document).ready(function () {
    adjust_result_position();
    $(window).resize(function () {
        adjust_result_position()
    });
    $(ls.form_anti_bot_id).val(ls.form_anti_bot);
    $(query).on("keyup", function (c) {
        var b = c.keyCode || c.which;
        if ($.trim(query.val()).length && b === 13) {
            if ((result.is(":visible") || result.is(":animated")) && result.find("tr").length !== 0) {
                if (query.selected_row !== undefined) {
                    $(result).find("tr").trigger("click")
                }
            } else {
                show_result()
            }
        } else {
            search_query(this, false, true)
        }
    });
    $(query).on("keydown", function (c) {
        var b = c.keyCode || c.which;
        if (b === 40 || b === 38) {
            if ($.trim(query.val()).length && result.find("tr").length !== 0) {
                if ((result.is(":visible") || result.is(":animated"))) {
                    result.find("tr").removeClass("hover");
                    if (query.selected_row === undefined) {
                        query.selected_row = result.find("tr").eq(0);
                        $(query.selected_row).addClass("hover")
                    } else {
                        $(query.selected_row).removeClass("hover");
                        if (b === 40) {
                            if ($(query.selected_row).next().length === 0) {
                                query.selected_row = result.find("tr").eq(0);
                                $(query.selected_row).addClass("hover")
                            } else {
                                $(query.selected_row).next().addClass("hover");
                                query.selected_row = $(query.selected_row).next()
                            }
                        } else {
                            if ($(query.selected_row).prev().length === 0) {
                                query.selected_row = result.find("tr").last();
                                query.selected_row.addClass("hover")
                            } else {
                                $(query.selected_row).prev().addClass("hover");
                                query.selected_row = $(query.selected_row).prev()
                            }
                        }
                    }
                } else {
                    if (b === 40) {
                        show_result()
                    }
                }
            }
        }
    });
    $(query).on("focus", function () {
        if ($.trim(query.val()).length && (result.is(":hidden") || result.is(":animated")) && result.find("tr").length !== 0) {
            search_query(this, false, true);
            show_result()
        }
    });
    $(result).on("mouseover", "tr", function () {
        result.find("tr").removeClass("hover");
        query.selected_row = this;
        $(this).addClass("hover")
    });
    $(result).on("mouseleave", "tr", function () {
        result.find("tr").removeClass("hover");
        query.selected_row = undefined
    });
    $(result).on("click", "tr", select_result);
    var a;
    $(document).bind("touchstart", function () {
        a = $(window).scrollTop()
    }).bind("touchend", function (c) {
        var d, b;
        d = a - $(window).scrollTop();
        b = $(document);
        b.addClass("touched");
        if (d < 10 && d > -10) {
            if (!$(c.target).closest(result).length && !$(c.target).is(query) && $(result).is(":visible")) {
                hide_result()
            }
        }
    });
    $(document).on("click", function (b) {
        if ($(this).hasClass("touched")) {
            $(this).removeClass("touched")
        } else {
            if (!$(b.target).closest(result).length && !$(b.target).is(query) && $(result).is(":visible")) {
                hide_result()
            }
        }
    });
    $(ls.form_id).submit(function () {
        return false
    });
    $(ls.arrow_class).on("click", function () {
        var b;
        if (this.id === ls.next_page_id) {
            if (parseInt(current_page.val(), 10) + 1 <= parseInt(total_page_lbl.html(), 10)) {
                b = parseInt(current_page.val(), 10) + 1
            } else {
                return
            }
        } else {
            if (parseInt(current_page.val(), 10) - 1 >= 1) {
                b = parseInt(current_page.val(), 10) - 1
            } else {
                return
            }
        }
        current_page.val(b);
        current_page_lbl.html(b);
        search_query(query[0], true, false)
    });
    $(page_range).on("change", function () {
        search_query(query[0], true, true)
    })
});

Any input on this matter will be much appreciated and a solution would be heaven.

Thanks in advance

  • 写回答

1条回答 默认 最新

  • weixin_33681778 2015-09-09 01:10
    关注

    I found a easy way by just including this little script on the bottom of my page

    <script type="text/javascript">
        window.onload=function() {
            var text_input = document.getElementById ('ID_of_element');
            text_input.focus ();
            text_input.select ();
        }
    </script>
    

    This highlights the text and searches.

    评论

报告相同问题?

悬赏问题

  • ¥15 arduino控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥85 maple软件,solve求反函数,出现rootof怎么办?
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题