2

好的,第二个问题,让我先说我对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

谢谢

4

1 回答 1

1

Update : Ok so the problem was just that you had to do a return redirect(); to ensure the false returns correctly. I made a fiddle here : http://jsfiddle.net/3UbLa/1/


I'm don't know anything about sphider search but from what you posted you should be able to combine the two like below:

Your JS will change like this:

<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>

Your html will change to :

<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> 
于 2013-04-26T16:09:55.047 回答