【问题标题】:Best practices to implement search function实现搜索功能的最佳实践
【发布时间】:2018-07-10 10:30:15
【问题描述】:

我找到了一些问题/答案,但它们都已经过时了,所以我想问问是否有一些新的做法/元数据。

我有这个网页我现在正在工作,atm 它唯一的首页,它建立在 html、css 和 javascript 之上。我在 Laravel 中构建,因为很快我将为表单、用户、文档和其他一些东西实现一个数据库。

无论如何,我想要一个简单的搜索引擎(一个只搜索不同页面的搜索引擎,例如,有点像 facebook 的搜索)

所以经过一段时间的思考和挖掘,这是我目前正在研究的解决方案:

HTML:

<ul class="navbar-nav ml-1 mr-5">
    <form class="form-inline" id="procurahead">
      <input class="form-control espaco_procura" type="text" list="listhead" placeholder="Pesquisa" aria-label="Search" id="txtinputhead" autocomplete="off">
      <datalist id="listhead">
        <!-- THEMES -->
        <option value="foo">
        <option value="foo1">
        <option value="foo2">
        <option value="foo3">
        <option value="etc">
      </datalist>
      <button class="btn btn-outline-success btnprocura" type="submit"><i class="material-icons procura">search</i></button>
    </form>
  </ul>

JAVASCRIPT:

$('#procurahead').submit(function(event){

event.preventDefault();

var input1 = $('#txtinputhead').val();

if(input1 == "foo"){
window.location.href = 'http://localhost/laravel/l55/public/foo';
}
if(input1 == "foo1"){
window.location.href = 'http://localhost/laravel/l55/public/foo1';
}
if(input1 == "foo2"){
window.location.href = 'http://localhost/laravel/l55/public/foo2';
}
if(input1 == "foo3"){
window.location.href = 'http://localhost/laravel/l55/public/foo3';
} 
if(input1 == "etc"){
window.location.href = 'http://localhost/laravel/l55/public/etc';
} 
});

所以,虽然我没有很多页面,但它工作得很好,但我认为如果我扩展到所有网页,那么我会很长,似乎应该有一些更简单的方法来做它。

所以问题是:有没有更好/更简单的方法来实现这一点?

提前致谢!

【问题讨论】:

  • 将您的 URL (http://localhost/laravel/l55/public/) 附加到 $('#txtinputhead').val()?
  • 让用户从给定的关键字列表中进行选择并不是真正的“搜索”,我会说更多的是导航。如果用户没有选择给定选项之一,而是输入了他们自己的术语 - 那么您仍然没有任何功能可以在此处实际搜索。
  • 是的,这是我一直在寻找的学习方法。

标签: javascript php html search


【解决方案1】:

从using javascript to detect whether the url exists before display in iframe 截取这段代码作为代码介绍,您可以构建如下内容:

function urlExists(url) {
    var request;
    if (window.XMLHttpRequest) {
        request = new XMLHttpRequest();
    } else {
        request = new ActiveXObject("Microsoft.XMLHTTP");
    }
    request.open('GET', url, false);
    request.send();
    if (request.status === 404) {
        return false;
    } else {
        return true;
    }
}
$('#procurahead').submit(function (event) {
    event.preventDefault();
    var input1 = $('#txtinputhead').val();
    var url = 'http://localhost/laravel/l55/public/' + input1;
    if (urlExists(url)) {
        window.location.href = url;
    }
}

我希望这是您所需要的。问候。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-10
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    • 2010-11-18
    • 1970-01-01
    相关资源
    最近更新 更多