gpt4 book ai didi

search - ASP.NET MVC - 构建搜索表单的最佳方法是什么,该表单可以在没有 JS 的情况下与 SEO 和用户一起正常工作?

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:35:42 24 4
gpt4 key购买 nike

我正致力于在我的应用程序中构建一个搜索引擎,因为我不想在我的 URL 中有一个查询字符串,我目前正在使用 Javascript 为我提交 searchTerms。

基本上我使用“表单”,而只是输入

<input id="searchBox" class="search" name="searchTerm" tabindex="1" onfocus=" this.className = 'search-alt'; if (this.value=='search...') this.value = ''" type="text" onkeypress="searchKeyPress(event,this.form)" maxlength="80" size="28" value="search...">

<script type="text/javascript">
function searchKeyPress(e, form) {
var key = e.keyCode || e.which;
if (key == 13) {window.location.href = '<%: url.Content("~/search") %>/' + $('#searchBox').val();}}
</script>

这种方法的问题是“双重”

  1. 如果用户没有Javascript,表单将不会提交
  2. 我不确定搜索引擎是否也能使用此搜索表单

所以我的问题是

Can I use a Form element on my page that can submit " http://example.com/search/{searchTerms} " instead of " http://example.com/search/?q={searchTerms} " while NOT using Javascript?

我正在使用 ASP.NET MVC 2

最佳答案

好的,我想我已经找到了解决方案。基本上,如果有查询字符串项,我会使用 RedirectToAction。

查看

 <form action="/search/" id="searchForm" method="get">
<input id="searchBox" class="search-gray" name="searchTerms" tabindex="1" onblur=" if (this.value==''){this.value = 'search...'; this.className = 'search-gray'}" onfocus=" this.className = ''; if (this.value=='search...') {this.value = ''}" type="text" maxlength="80" size="28" value="search...">
</form>

Controller

    Function Index(Optional ByVal searchTerms As String = "") As ActionResult

If Not Request.QueryString("searchTerms") = "" Then
Return RedirectToAction("Index", "Search", New With {.searchTerms = searchTerms})
End If

ViewData("searchTerms") = searchTerms
Return View()
End Function

没有更多的 javascript。

关于search - ASP.NET MVC - 构建搜索表单的最佳方法是什么,该表单可以在没有 JS 的情况下与 SEO 和用户一起正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3536171/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com