2015-09-04 92 views
0

我有一個使用php的ajax實時搜索腳本,可以在您鍵入時自動搜索數據庫。頁面加載時的Ajax搜索

但是我現在試圖使用GET函數將一個值從url中放入搜索框。一切工作正常,價值被放置在輸入框,但它不會搜索,除非你輸入。

如何在頁面加載時使用url的值來搜索它?

這裏是AJAX腳本:

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) 
    }) 
}); 

在這個問題上的任何輸入將被大加讚賞和解決方案將是天堂。

在此先感謝

回答

0

我剛好包括在我的網頁

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

底部這個腳本這凸顯了文本和搜索發現了一個簡單的方法。