doupeng8419 2013-06-12 23:12
浏览 74
已采纳

我的javascript函数不会重定向到其他页面

I have two items first is a dropdownbox that once user selects its options will trigger a javascript code and show the results on the same page,

the second one is a input box and a search button, once user type something and click on search button, it triggers the javascript but for some reasons it adds the value to the current page address rather than redirecting to other page and xmlhttp.status returns 0.

Rather than redirecting to class/myresults.php it add the item1 and its value to the current page address which is www.myexample.com/index.php?item1=computer

My form that does not work

<form action="">
               <input name="search" type="text" title="Search"/>
               <input type="submit" value="search" onclick="finditem(this.form.search.value)"/>
</form>


function finditem(option){
    alert(option); <<<<shows the correct entered value
     if (window.XMLHttpRequest)
        {
            xmlhttp=new XMLHttpRequest();
        }
        else
        {
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function()
        {
            alert(xmlhttp.status);  << returns 0
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                document.getElementById("Result").innerHTML=xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET","class/myresults.php?item1="+option,true);
        xmlhttp.send();
    }

java script of the drop down box that works

<select name="items" onchange="findbox(this.value)">
   .....
  </select>

 function findbox(option){
             if (window.XMLHttpRequest)
                {
                    xmlhttp=new XMLHttpRequest();
                }
                else
                {
                    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                }
                xmlhttp.onreadystatechange=function()
                {
                    if (xmlhttp.readyState==4 && xmlhttp.status==200)
                    {
                        document.getElementById("Result").innerHTML=xmlhttp.responseText;
                    }
                }
                xmlhttp.open("GET","class/myresults.php?item2="+option,true);
                xmlhttp.send();

            }
  • 写回答

1条回答 默认 最新

  • doumu6997 2013-06-12 23:22
    关注

    Your form gets submitted, i.e. the browser posts the data to the same location where it found your page. You do not seem to want that. Therefore you should prevent the submittal of the form. To accomplish that, you should not listen to the onclick event of the button but to the onsubmit event of the form:

    <form action="" onsubmit="return finditem(this.search.value)">
                   <input name="search" type="text" title="Search"/>
                   <input type="submit" value="search"/>
    </form>
    

    And here's the JavaScript: (Notice that finditem returns false to prevent the form from actually submitting.)

    function finditem(option){
         if (window.XMLHttpRequest)
            {
                xmlhttp=new XMLHttpRequest();
            }
            else
            {
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange=function()
            {
                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    document.getElementById("Result").innerHTML=xmlhttp.responseText;
                }
            }
            xmlhttp.open("GET","class/myresults.php?item1="+option,true);
            xmlhttp.send();
    
            return false;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值