jquery 表单提交并在 div 中显示结果而无需重新加载
美好的一天,
我在 stackoverflow 上找到了下面的一些代码,但我一生都无法让它工作。我已经玩了一点代码,但我仍在学习 javascript (慢慢地)和 jquery。
我想做的是有一个带有搜索输入的搜索 div,但我不想重新加载页面,而是希望在搜索下方有一个 div 来显示结果,而不需要重新加载。输入搜索内容并单击“搜索”。这是我的代码,它不执行任何操作,我可能会添加:
我的 jquery 版本是 1.5.2,并加载了 include("javascript.php");
<?php
include("search_div.php");
include("javascript.php");
?>
<script type="text/javascript">
$("search").submit(function() {
$.post($(this).attr("action"), $(this).serialize(), function(html) {
$("#results").html(html);
});
return false; // prevent normal submit
});
</script>
<div id="content">
<div id="searchdiv">
<form type="submit" onsubmit="return false;" name="search" id="search" method="post">
<table border="0" width="850" id="searchquery" cellpadding="0" cellspacing="0">
<tr><td align="center">Search Query: <input type="text" size="50" id="q" name="q"></td></tr>
</table>
<table border="0" width="850" id="searchquerybuttons" cellpadding="0" cellspacing="0">
<td align="right" width=840><input type="Submit" value="Search">
</table>
</form>
</div> <!-- end search div -->
<div id="results">
<?php
if (isset($_POST['q'])) {
echo "<br><br>Query is: ".$_POST['q'];
}
?>
</div> <!-- End results div -->
</div> <!-- End content div -->
<?php
include ("footer.php");
?>
</div> <!-- End of container div -->
Good day,
I found some of the code below on stackoverflow, but for the life of me I can't get this to work. I've played with the code a little bit, but I'm still learning javascript (slowly) and jquery.
What I'd like to do, is to have a search div with a search input, but instead of reloading the page, I'd like a div below the search to display the results, without a reload. Type in a search and click 'Search'. Here is my code, which does nothing, I might add:
My jquery version is 1.5.2 and is loaded with the include("javascript.php");
<?php
include("search_div.php");
include("javascript.php");
?>
<script type="text/javascript">
$("search").submit(function() {
$.post($(this).attr("action"), $(this).serialize(), function(html) {
$("#results").html(html);
});
return false; // prevent normal submit
});
</script>
<div id="content">
<div id="searchdiv">
<form type="submit" onsubmit="return false;" name="search" id="search" method="post">
<table border="0" width="850" id="searchquery" cellpadding="0" cellspacing="0">
<tr><td align="center">Search Query: <input type="text" size="50" id="q" name="q"></td></tr>
</table>
<table border="0" width="850" id="searchquerybuttons" cellpadding="0" cellspacing="0">
<td align="right" width=840><input type="Submit" value="Search">
</table>
</form>
</div> <!-- end search div -->
<div id="results">
<?php
if (isset($_POST['q'])) {
echo "<br><br>Query is: ".$_POST['q'];
}
?>
</div> <!-- End results div -->
</div> <!-- End content div -->
<?php
include ("footer.php");
?>
</div> <!-- End of container div -->
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你检查过输出的 HTML 源吗?
您是否检查了 javascript 错误控制台以查看错误是什么(如果有)?
javascript.php
的内容是什么?您不能将 javascript 库包含在 PHP 中。您应该使用类似的内容:另外,this:
应该是:
并且您应该从
Did you check the outputted HTML source?
Did you check the javascript error console to see what the errors were, if any?
What are the contents of
javascript.php
? You can't include a javascript library with PHP. You should use something like:Also, this:
should be:
And you should remove the onsubmit attribute from the
<form>
HTML.