我使用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"的列。
提前致谢!
<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);
}
?>
答案 0 :(得分:1)
在Regex normalRegex = new Regex(@" \.\.\. .*?>(.*?)<\/a>", RegexOptions.SingleLine);
Regex secondaryRegex = new Regex(@"page=.*?"">(.*?)</a>\n.*?<a class=""pagebtn""");
int valueFound;
bool numberParsed;
if (normalRegex.IsMatch(myInput)) {
// your code to check here
// use int.TryParse to parse your value and add the result to
numberParsed = int.TryParse(m2[0].Groups[1].Captures[0].Value, out valueFound);
}
if (!numberParsed) {
if (secondaryRegex.IsMatch(myInput)) {
// second code matches
} else {
// no match
}
}
中,您尝试访问在另一个函数中定义的局部变量createq()
。如果在函数内声明变量,则只有该函数可以访问变量。
您可以通过将txt作为参数传递给createq来解决此问题。为此,您需要自己调用txt
,而不是将其设置为点击事件的事件处理程序。
使用jqoery的.click()为click事件添加适当的事件处理程序,并从该处理程序调用{{1}}添加,传递createq
的值。为了设置点击处理程序,您需要引用ID为“create”的元素,这是您目前没有的。
此特定问题的解决方案如下所示:
createq
答案 1 :(得分:0)
关于页面刷新的方式。如果缺少id为window.location.href的DOM元素,页面将刷新。例如 假设你有window.location.href =“#div1”; 如果缺少id =“div1”的DOM元素,页面肯定会刷新。