我使用JQuery创建了一个类似谷歌搜索的即时搜索。突出显示的代码不起作用。这很奇怪,因为他们自己工作得很好,其他一切都很好。知道为什么会这样吗? Q1。
searchq()工作正常,但createq()函数不起作用,变量txt可以发布到其他文件(search.php)。但是,函数createq()无法POST。它在测试后确实获得了全局变量txt,但无论我使用什么POST方法,php文件(create_object.php)都无法获取它。任何人都可以帮助编写一些可以在我的代码中使用的POST代码。
Q2 我想创建一个函数,当按下回车时,用户将被重定向到第一个搜索结果(用网址锚定)。为了实现这一点,我创建了一个函数,变量redirectUrl将锚定的url作为字符串,但是,重定向函数window.location.href不起作用,页面只是刷新。我在另一个文件中测试了window.location.href函数,但是它可以工作。很奇怪,我的页面只是刷新,当我指向谷歌时它甚至刷新。 window.location.href(" www.google.com&#34)。
请注意,我没有在此处包含连接数据库功能。因为我认为数据库用户名和密码设置会与你的不同。所以如果你想测试它,请创建你自己的。 mysql设置了一个名为" objects"的表,它有一个名为" name"的列。
提前致谢!
<?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文件(search.php)
<?php
if(isset($_POST["createVal"])){
$name=$_POST["createVal"];
var_dump($name);
}
?>
php文件(create_object.php)
mixed_dists = function(n, n_means, var=0.2) {
means = rnorm(n_means, mean=1, sd=2)
values <- NULL
class <- NULL
for (i in 1:n_means) {
temp <- rnorm(n/n_means, mean=means[i], sd=0.2)
class <- c(class, rep(i, n/n_means))
values <- c(values, temp)
}
return(list(values, class));
}
N = 100
#Scenario 1: The training data in each class were generated from bivariate Gaussian distributions
#with uncorrelated components and different means.
scenario1 = function () {
var = 0.5
n_groups = 2
m = mixed_dists(N, n_groups, var=var)
x = m[[1]]
group = m[[2]]
y = mixed_dists(N, n_groups, var=var)[[1]]
data = matrix(c(x,y, group), nrow=N, ncol=3)
colnames(data) = c("x", "y", "group")
data = data.frame(data)
plot(x=data$x,y=data$y, col=data$group)
model = lm(y~x, data=data)
summary(model)
}
#Scenario 2: The training data in each class came from a mixture of 10
#low-variance Gaussian distributions, with individual means themselves
#distributed as Gaussian.
scenario2 = function () {
var = 0.2 # low variance
n_groups = 10
m = mixed_dists(N, n_groups, var=var)
x = m[[1]]
group = m[[2]]
y = mixed_dists(N, n_groups, var=var)[[1]]
data = matrix(c(x,y, group), nrow=N, ncol=3)
colnames(data) = c("x", "y", "group")
data = data.frame(data)
plot(x=data$x,y=data$y, col=data$group)
model = lm(y~x, data=data)
summary(model)
}
# scenario1()
# scenario2()
答案 0 :(得分:0)
尝试使用id
绑定输入var txt = $("input").val();
<input type="text" name="search" style="width:400px " placeholder="Search box" onkeyup="searchq();">
将上述内容更改为
var txt = $("#searchinput").val();
<input type="text" id="searchinput" name="search" style="width:400px " placeholder="Search box" onkeyup="searchq();">
我认为你试图在这里显示搜索结果
<div id="output"></div>
和jQuery绑定就是你的代码
$("#search_output").html("");
所以将HTML更改为此
<div id="search_output"></div>
这也在我们的代码中
$("#search").keypress(function(evt){
没有HTML元素与它绑定,我认为你试图将它与搜索输入绑定,所以将上面的内容更改为
$("#searchinput").keypress(function(evt){
上述更改还应解决window.location.href not working
问题
所以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>
和脚本将
<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>
注意:如果你检查浏览器控制台,你可能会看到一些错误,也有一些错字错误,例如你的JS中缺少;
。
在PHP中,这里
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>";
}
}
点击 $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>";
}
}
答案 1 :(得分:0)
两件事:
未定义输入搜索ID,$(&#34; #search&#34;)。按键无法正常工作。改为:
&LT;输入类型=&#34;文字&#34;命名=&#34;搜索&#34; id =&#34;搜索&#34; 样式=&#34;宽度:400px&#34;占位符=&#34;搜索框&#34;的onkeyup =&#34; SEARCHQ();&#34; &GT;
Div id&#34; output&#34;,应该是&#34; search_output&#34;,如$(&#34;#search_output&#34;)中所要求的那样。改为:
&LT; div id =&#34; search_output &#34; &GT; &LT; / div&gt;