首页 > 解决方案 > 博主:带参数的html搜索

问题描述

我有一个问题,我在博客中创建模板,并在那里搜索带有标签的文本需要这个 url:

www.my_blog.blogspot.com/search?q=label:Printers+SearchText

我有这个代码:

<form action='/search' class='search' method='GET'>
	<input name='q' placeholder='Пошук' type='text'/>
	<input id='search_button' name='' title='Search' type='submit' value=''/>
</form>

结果:

www.my_blog.blogspot.com/search?q=SearchText

但我需要使用以下参数进行搜索:

www.my_blog.blogspot.com/search?q=label:Printers+SearchText

我怎么能做到这一点,没有javascript?

标签: htmlblogger

解决方案


我建议(如果您不想使用 JS 来处理查询连接)使用隐藏输入创建多个查询属性:

www.my_site.com/search? 类别=打印机& q=搜索文本

<form action='/search' class='search' method='GET'>
    <input name='category' value='Printers' type='hidden'>
    <input name='q' placeholder='Пошук' type='text'>
    <input id='search_button' name='' title='Search' type='submit'>
</form>

嗯,如果你真的,真的需要一个动态连接(前缀)的查询参数值,例如:

www.my_site.com/search? q=类别:打印机+搜索文本

这可以通过添加一些 JS 来实现(为了简单起见,我将使用无所不在的 jQuery 库)

$('form.search').on('submit', function (e) {

  // Prevent default form submit
  // since we need to cunstruct a prefixed param value
  e.preventDefault(); 

  // Set prefix and get SearchText
  var pfx = "Category:Printers+";
  var val = $(this).find('[name=q]').val();

  $.ajax({
    url: this.getAttribute("action"),
    data: { q : pfx + val }   // Join prefix to value into "q" param
  }).done(function(res) {
     console.log(res); // Success
  }); 
});

推荐阅读