重定向到特定的url基于用户输入,但如果没有预定义的关键字存在搜索网站

Redirect to specific url based on user input but if no pre-defined keyword exists search the site

本文关键字:输入 如果没有 预定义 存在 关键字 搜索网站 url 重定向 于用户 用户      更新时间:2023-09-26

好的,第二个问题,首先我要说的是,我对javascript一无所知,我使用的所有东西都只是从这里和那里剪切和粘贴,所以请原谅我:-p.

我有一个客户谁想要建立一个搜索框,如果有人在某个预定义的关键字,它去到一个特定的页面。我从这里截取了这段代码来完成这部分:

 <form id='formName' name='formName' onsubmit='redirect();return false;'>
            <input type='text' id='userInput' name='userInput' value=''>
            <input type='submit' name='submit' value='Submit'>
        </form>
        <script type='text/javascript'>
        function redirect() {
           var input = document.getElementById('userInput').value.toLowerCase();
            switch(input) {
                case 'keyword1':
                    window.location.replace('page1.html');
                    break;
                case 'keyword2':
                    window.location.replace('page2.html');
                    break;
                default:
                    window.location.replace('error.html');
                    break;
            }

        }
        </script>

现在我也使用sphider php搜索脚本,我使用这个嵌入的形式:

<form action="search/search.php" method="get">
<input type="text" name="query" id="query"  value="SEARCH" columns="2" 
autocomplete="off" delay="1500"  
onfocus="if(this.value==this.defaultValue)this.value=''"   
onblur="if(this.value=='')this.value=this.defaultValue" >
<input type="submit" value="" id="submit">
<input type="hidden" name="search" value="1">
</form> 

是否有任何方法我可以结合两者,以便如果他们搜索预定义的关键字,他们得到定向到正确的页面,但如果他们的搜索不包含预定义的关键字,它搜索sphider?

对不起,如果这个问题是可笑的,就像我说的,我是新来的:-p

谢谢

Update:好的,所以问题只是你必须做一个return redirect();来确保正确的错误返回。我在这里做了一个小提琴:http://jsfiddle.net/3UbLa/1/


我对sphider搜索一无所知,但从你发布的内容来看,你应该能够像下面这样将两者结合起来:

你的JS会像这样改变:

<script type='text/javascript'>
        function redirect() {
           //look for text inside the NEW textbox
           var input = document.getElementById('query').value.toLowerCase();
            switch(input) {
                case 'keyword1':
                   //put this to see if this code runs 
                   //alert("Test");// UNCOMMENT THIS
                    window.location.replace('page1.html');
                    break;
                case 'keyword2':
                    window.location.replace('page2.html');
                    break;
                default://no keyword detected so we submit the form.
                    return true;
                    break;
            }
            return false;//don't let the form submit
        }
</script>

你的html将变成:

<form action="search/search.php" method="get" 
                              onsubmit='return redirect();'>
<!-- pretty much the same thing except you remove the return false  !-->
<input type="text" name="query" id="query"  value="SEARCH" columns="2" 
autocomplete="off" delay="1500"  
onfocus="if(this.value==this.defaultValue)this.value=''" 
onblur="if(this.value=='')this.value=this.defaultValue" >
<input type="submit" value="" id="submit">
<input type="hidden" name="search" value="1">
</form> 
相关文章: