2016-11-23 85 views
0

我有一个简单的形式是这样的:HTML链接,追加查询字符串到当前URL

<form> 
     <input name="search"> 
     <button type="submit">Search...</button> 
     Price: <a href="?filter=price&direction=asc">Low</a> | 
      <a href="?filter=price&direction=desc">High</a> 
</form> 

注意inputsearch名称可以搜索一个关键字,而<a>链接作为过滤器工作。当您提交表单时,网址将会是localhost/products?search=smartphone。通过我的服务器端脚本,这将生成一个页面,其中包含名称中包含单词smartphone的所有产品。

现在说我在localhost?search=smartphone。如果我点击Low,它会产生

http://localhost/products?filter=price&direction=asc 
    -> filter all products with price asc 

不过,我真的很打算得到的是

http://localhost/products?search=smartphone&filter=price&direction=asc 
    -> search for 'smartphone', and filter only those results by price asc 

我要如何才能到达searchfilter结合(其ascdesc方向) ?在旁注中,这在搜索/过滤方面是否有效?我应该将<a>更改为<input>?我几乎没有搜索机制的经验,所以任何建议,将不胜感激!

回答

0

使用

onclick="form.submit();" 


<form id="my_form"> 
    <input name="search"> 

    Price: <a href="?search="+ escape(document.forms.my_form.search.value)+"&filter=price&direction=asc" onclick="document.getElementById('my_form').submit(); return false;">Low</a> | 
     <a href="?search="+ escape(document.forms.my_form.search.value)+"&filter=price&direction=desc" onclick="document.getElementById('my_form').submit(); return false;">High</a> 

+0

这是行不通的。它要么给我'http:// localhost/products?search = some_value'或'http:// localhost/products?filter = name&direction = asc'。它需要有'过滤器'和'搜索' – Alex

+0

更新我的答案。现在检查。 –