Javascript 需要使用搜索栏中的搜索结果填充表
我有一个网页,有一个搜索栏和一堆下拉菜单,但现在只有搜索栏很重要。不管怎么说,当我点击搜索栏后的go按钮时,它会显示一个表格,但不会像我想的那样将搜索项放入表格中。我通过Python使用SimpleHTTPServerJavascript 需要使用搜索栏中的搜索结果填充表,javascript,python,html,css,simplehttpserver,Javascript,Python,Html,Css,Simplehttpserver,我有一个网页,有一个搜索栏和一堆下拉菜单,但现在只有搜索栏很重要。不管怎么说,当我点击搜索栏后的go按钮时,它会显示一个表格,但不会像我想的那样将搜索项放入表格中。我通过Python使用SimpleHTTPServer <form role="form" id="form"> <fieldset> <div class="form-group"> <div class="row">
<form role="form" id="form">
<fieldset>
<div class="form-group">
<div class="row">
<label for="search"> </label>
<div class="col-lg-7 col-lg-offset-2">
<input type="text"
class="form-control" name="search" id="search"
placeholder="Search for..." />
</div>
<div class="col-lg-2">
<br>
<button type="button" class="btn btn-success" id="submit"> Go!
</button> </div> </div> </div>
</fieldset>
</form>
走!
JS:
$(“#提交”)。单击(函数(e){
e、 预防默认值()
var search=$('#search').val();
$.get('post2.html',函数(returndata){
$('#output').html(返回数据);
} );
});
以前的搜索:
document.write(搜索)
您的“搜索”超出范围。它仅存在于的匿名函数中。单击()。您需要将“搜索”传递到HTML中,但在当前的设置中,这是不可能的
我建议使用Handlebar之类的工具,它允许您使用变量占位符定义模板化HTML,编译它们,然后插入变量值。例如,您可以在HTML中定义:
<script type="text/x-handlebars-template" id="table-data-template">
<table class="table">
<thead>
<tr>
<th> Previous Searches: </th>
</tr>
</thead>
<tr>
{{data}}
</tr>
</table>
</script>
超级干净。但你得花点时间阅读车把之类的东西
如果你没有在你的应用程序中做很多基于模板的工作,因此手柄可能有点过分,你可以简单地在JS中定义HTML模板,如下所示:
$('#submit').click(function(e){
e.preventDefault();
var search=$('#search').val();
$('#output').html(
"<table class='table'>
<thead>
<tr>
<th> Previous Searches: </th>
</tr>
</thead>
<tr>" + search +
"</tr>
</table>");
$(“#提交”)。单击(函数(e){
e、 预防默认值();
var search=$('#search').val();
$('#output').html(
"
以前的搜索:
“+搜索+
"
");
因此,从字面上写出将被注入到输出中的HTML字符串,并将搜索结果浓缩到其中。不是超级干净,但如果只执行一次,就可以完成任务。您的“搜索”超出范围。它只存在于.click()的匿名函数中。您需要通过“搜索”但这在当前的设置中是不可能的
我建议您使用Handlebar之类的工具,它允许您使用变量占位符定义模板化HTML,编译它们,然后插入变量值。例如,您可以在HTML中定义:
<script type="text/x-handlebars-template" id="table-data-template">
<table class="table">
<thead>
<tr>
<th> Previous Searches: </th>
</tr>
</thead>
<tr>
{{data}}
</tr>
</table>
</script>
超级干净。但是你得花点时间阅读车把之类的东西
如果你没有在你的应用程序中做很多基于模板的工作,因此手柄可能有点过分,你可以简单地在JS中定义HTML模板,如下所示:
$('#submit').click(function(e){
e.preventDefault();
var search=$('#search').val();
$('#output').html(
"<table class='table'>
<thead>
<tr>
<th> Previous Searches: </th>
</tr>
</thead>
<tr>" + search +
"</tr>
</table>");
$(“#提交”)。单击(函数(e){
e、 预防默认值();
var search=$('#search').val();
$('#output').html(
"
以前的搜索:
“+搜索+
"
");
因此,从字面上写出将被注入到输出中的HTML字符串,并将搜索结果浓缩到其中。不是超级干净,但如果只执行一次,就可以完成任务。从开始的代码位于一个名为“post2.HTML”的文件中,该文件由get函数调用。我还希望该表不断增长d如果有人知道如何做,则保留以前的搜索结果从开始的代码位于一个名为“post2.html”的文件中,该文件由get函数调用。我还希望表格不断增长,如果有人知道如何做,则保留以前的搜索结果