【问题标题】:How to change the value of search input box to the user input value如何将搜索输入框的值更改为用户输入的值
【发布时间】:2013-11-07 16:16:31
【问题描述】:

这里是代码

<form class="navbar-search pull-right header-link" method="post" name="searchportlet" id="searchportlet" onsubmit="window.location.href='/s.nl?ext=F&amp;sc=22&amp;category=&amp;search='+escape(document.forms['searchportlet'].elements['search'].value);return false;">
    <div class="dowith clearfix">
        <a href="#" class="left" onclick="window.location.href='/s.nl?ext=F&sc=22&category=&search='+escape(document.forms['searchportlet'].elements['search'].value);return false; return false;">
            <img alt="" src="/images/search.png" style="margin-top: 5px;">
        </a>
        <input name="search" type="text" onsubmit="window.location.href='/s.nl?ext=F&sc=22&category=&search='+escape(document.forms['searchportlet'].elements['search'].value);return false; return false;" onfocus="if (this.value == 'Search entire store...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Search entire store...';}" value="Search entire store..." class="right textfield" id="search_input">
    </div><!--End dowith-->
</form>

如何保留用户输入的搜索输入文本。我需要 js 或 jquery 中的解决方案。表单提交后搜索整个商店值必须替换为用户输入的文本

【问题讨论】:

  • 保留什么时候?在哪里?为什么?
  • 我试过这个脚本,但它不工作
  • 你的意思是,表单提交后,页面刷新,搜索返回,那么搜索框会保留原来的搜索字符串。
  • @user1881845 - 你的问题很难理解。请花点时间详细解释设置、预期行为和实际行为。类似于:1)我加载页面 2)我在“搜索”输入字段中输入一个值 3)我提交表单 4)预期行为:以前输入的值显示在“搜索”输入字段中 5)实际行为:以前输入值不会显示在“搜索”输入字段中。
  • 这正是@PatDobson 上面所建议的。为此,您不需要 Javascript 或 jQuery。这需要在服务器端通过为输入字段赋予属性value="My entered search string" 来处理

标签: javascript jquery search


【解决方案1】:

查看您的网站,在您的页面加载后执行此部分:

jQuery(document).ready(function($) {

在它下面添加类似这样的东西:

$('#search_input').val(location.href.split('&search=')[1] || '');

这不是一个好的解决方案(基本上是读取 URL 并选择搜索词(如果存在))。 如果你可以编辑服务器端代码,最好在那里处理。

【讨论】:

  • 奇怪的是 jQuery 现在无法在您的网站上运行。无论如何在文档末尾添加这个并告诉我它是否有效? &lt;script&gt;document.getElementById('search_input').value = location.href.split('&amp;search=')[1] || '';&lt;/script&gt;
  • 我写了这个 可以吗?
  • 没有放在ready函数的第一行:jQuery(document).ready(function(yq) { yq('#search_input').val(location.href.split('&amp;search=')[1] || ''); })
猜你喜欢
  • 1970-01-01
  • 2016-09-11
  • 2015-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-15
  • 2019-06-03
相关资源
最近更新 更多