Javascript有两个奇怪的问题:POST不工作,window.location.href不工作
我使用JQuery创建了一个类似于google搜索的即时搜索。突出显示的代码不起作用。这是奇怪的,因为他们自己的工作很好,其他一切都很好。知道为什么会这样吗? 问题1 searchq()工作正常,但createq()函数不起作用,变量txt可以发布到其他文件(search.php)。但是,函数createq()无法发布。它在测试后确实得到了全局变量txt,但是无论我使用什么POST方法,php文件(create_object.php)都无法得到它。有谁能帮我写一段可以在我的代码中使用的POST代码吗 问题2 我想创建一个函数,当按下enter键时,用户将被重定向到第一个搜索结果(用url锚定)。为了实现这一点,我创建了一个函数,使变量redirectUrl将锚定url作为字符串,但是,重定向函数window.location.href不起作用,页面只是刷新。我在另一个文件中自己测试了window.location.href函数,但它可以工作。这是如此奇怪,我的网页只是刷新,甚至刷新时,我直接到谷歌。window.location.href(“www.google.com”) 请注意,我没有在这里包含连接到数据库功能。因为我认为数据库用户名和密码设置与您的不同。因此,如果您想测试它,请创建您自己的。mysql设置了一个名为“objects”的表,其中有一列名为“name” 提前谢谢Javascript有两个奇怪的问题:POST不工作,window.location.href不工作,javascript,php,jquery,Javascript,Php,Jquery,我使用JQuery创建了一个类似于google搜索的即时搜索。突出显示的代码不起作用。这是奇怪的,因为他们自己的工作很好,其他一切都很好。知道为什么会这样吗? 问题1 searchq()工作正常,但createq()函数不起作用,变量txt可以发布到其他文件(search.php)。但是,函数createq()无法发布。它在测试后确实得到了全局变量txt,但是无论我使用什么POST方法,php文件(create_object.php)都无法得到它。有谁能帮我写一段可以在我的代码中使用的POST代
<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" style="width:400px " placeholder="Search box" onkeyup="searchq();">
<div id="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>
函数searchq(){
//获取值
var txt=$(“输入”).val();
//张贴价值
如果(txt){
$.post(“search.php”,{searchVal:txt},函数(结果){
$(“#搜索输出”).html(上面未找到结果+”
“创建”);
});
}
否则{
$(“#搜索输出”).html(“”);
}
};
函数createq(){
//allert for test purpose:测试createq函数是否已获取txt
警报(txt);
**$.post(“create_object.php”,{createval:txt})**
}
//如果按enter键,将页面重定向到第一个搜索结果
$(“#搜索”)。按键(功能(evt){
如果(evt.which==13){
//在DOM中查找第一个搜索结果并触发单击事件
var redirectUrl=$('#search_output').find('a').first().attr('href');
警报(重定向URL);
**window.location.href=“www.google.com”;
window.location.href=“www.google.com”**
}
})
PHP文件(search.PHP)
尝试用id绑定输入
var txt = $("input").val();
<input type="text" name="search" style="width:400px " placeholder="Search box" onkeyup="searchq();">
因此,将HTML更改为
<div id="search_output"></div>
它没有HTML元素绑定,我想您正在尝试用搜索输入绑定它,所以请更改上面的内容
var txt = $("#searchinput").val();
<input type="text" id="searchinput" name="search" style="width:400px " placeholder="Search box" onkeyup="searchq();">
$("#searchinput").keypress(function(evt){
上述更改还应解决window.location.href不工作的问题
因此HTML将是
<form method="POST">
<input type="text" id="searchinput" name="search" style="width:400px " placeholder="Search box" onkeyup="searchq();">
<div id="search_output"></div>
</form>
$output.
点有误,因此将其更改为以下内容
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>";
}
}
if($count==0){
$output=“没有搜索结果”;
}
否则{
while($row=mysqli\u fetch\u assoc($query)){
$object_name=$row[“name”];
$output=“”;
}
}
两件事:
未定义输入搜索id,$(“#搜索”)。按键不起作用。改为:
id=“search”style=“width:400px”placeholder=“search box”onkeyup=“searchq();”>
Div id“output”应为“search_output”,如美元所需(“search_output”)。改为:
搜索\u输出””>
你应该把这两个问题分成两篇不同的文章。他们之间没有太大的关联,回答一个问题的人不会得到完全的认可,这会让人们远离回答你的问题。只是一个友好的提示。对于creatq()
工作,请查看
<div id="search_output"></div>
$("#search").keypress(function(evt){
$("#searchinput").keypress(function(evt){
<form method="POST">
<input type="text" id="searchinput" name="search" style="width:400px " placeholder="Search box" onkeyup="searchq();">
<div id="search_output"></div>
</form>
<script type="text/javascript">
function searchq(){
// get the value
var txt = $("#searchinput").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
$("#searchinput").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>
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>";
}
}
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>";
}
}