Javascript两个奇怪的问题:POST不工作,window.location.href不工作


Javascript two weird problems: POST not working, window.location.href not working

我使用JQuery创建了一个类似于google搜索的即时搜索。高亮显示的代码不起作用。这很奇怪,因为它们自己工作得很好,其他一切都很好。知道为什么会这样吗?Q1。

searchq()可以正常工作,但是createq()函数不起作用,并且变量TXT可以发布到其他文件(search.php)。然而,createq()函数不能POST。它在测试后确实获得了全局变量txt,但是无论我使用什么POST方法,php文件(create_object.php)都无法获得它。谁能帮助写一个位POST代码,可以在我的代码工作。

第二季我想创建一个函数,当按下回车键时,用户将被重定向到第一个搜索结果(这是锚定的url)。为了实现这一点,我创建了一个函数,变量redirectUrl将锚定的url作为字符串,但是,重定向函数window.location.href不起作用,页面只是刷新。我在另一个文件中测试了window.location.href函数,它可以工作。奇怪的是,我的页面只是刷新,甚至当我直接到谷歌。window.location.href("www.google.com")。

注意,这里没有包含连接到数据库的函数。因为我认为数据库的用户名和密码设置和你的不一样。所以如果你想测试它,请创建你自己的。在mysql中设置了一个名为"objects"的表,它有一列名为"name"。

提前感谢!

 <html>
    <!-- google API reference -->
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <!-- my own script for search function -->
    <center>
    <form method="POST">
        <input type="text" name="search" id="search" style="width:400px " placeholder="Search box" onkeyup="searchq();">
        <div id="search_output">
        </div>
    </form>
    </center>   
      <!-- instant search function -->
 <script type="text/javascript">
function searchq(){
        // get the value
            var txt = $("input").val();
            // post the value
            if(txt){
                $.post("search.php", {searchVal: txt}, function(result){
                    $("#search_output").html(result+"<div id='"create'" onclick='"creatq()'"><br>Not found above? Create.</div>");
                });
            }
            else{
                $("#search_output").html("");
            }

        };
function createq(){
    // allert for test purpose: test if the txt has got by the createq function
    alert(txt);
    **$.post( "create_object.php",{creatVal:txt} );**
}
// if enter key pressed, redirect page to the first search result
$("#search").keypress(function(evt){
    if (evt.which == 13) {
       // find the first search result in DOM and trigger a click event 
        var redirectUrl = $('#search_output').find('a').first().attr('href');
        alert(redirectUrl);
      **window.location.href = "www.google.com";
window.location.href = "www.google.com";**
    }
})
</script>
    </html>
PHP文件(search.php)
 <?php
if(isset($_POST["searchVal"])){
    //get the search
    $search=$_POST["searchVal"];
    //sort the search
    $search=preg_replace("#[^0-9a-z]#i","",$search);
    //query the search
    echo "<br/>SELECT * from objects WHERE name LIKE '%$search%'<br/>";
    $query=mysqli_query($conn,"SELECT * from objects WHERE name LIKE '%$search%'") or die("could not search!");
    $count=mysqli_num_rows($query);
    //sort the result
    if($count==0){
        $output="there was no search result";
    }
    else{
        while($row=mysqli_fetch_assoc($query)){
            $object_name=$row["name"];
            $output="<div><a href='##'>".$object_name."</a></div>";
        }
    }
    echo $output;
}
?>
php文件(create_object.php)
 <?php
    if(isset($_POST["createVal"])){
        $name=$_POST["createVal"];
        var_dump($name);
    }
?>

createq()中,您试图访问在另一个函数中定义的局部变量txt。如果在函数中声明变量,则只有该函数可以访问该变量。

可以通过将txt作为参数传递给createq来解决这个问题。为了做到这一点,你需要自己调用createq,而不是将其设置为单击事件的事件处理程序。

使用jquery的.click()为click事件添加适当的事件处理程序,并从该处理程序调用createq,传递txt的值。为了设置click处理程序,您需要引用id为"create"的元素,而您目前还没有这个元素。

这个特殊问题的解决方案看起来像这样:

$.post("search.php", {searchVal: txt}, function(result){
    $("#search_output").html(result+"<div id='"create'"><br>Not found above? Create.</div>");
    $("#search_output create").click(function() {
       createq(txt);
    });
});
...
function createq(txt){
    ...
}

关于页面刷新方式。如果id为window.location.href的DOM元素丢失,页面将刷新。例如假设你有window.location.href="#div1";如果缺少id="div1"的DOM元素,页面一定会刷新。