【问题标题】:Search Form Error sends to ?q=搜索表单错误发送到 ?q=
【发布时间】:2015-07-02 06:42:00
【问题描述】:

我正在使用 Bootstrap 主题,我的表单是这样的:

<form class="navbar-form" role="search" method="get" action="<?=$domain?>search" onsubmit="DoSubmit();">
    <div class="input-group">
        <input type="text" class="form-control input" placeholder="Search" name="q">
        <div class="input-group-btn">
            <button  type="submit" class="btn btn-primary"><i class="glyphicon glyphicon-search"></i></button>
        </div>
    </div>
</form>

原来放置的页面是

http://www.domain.com/toys/search/Electronics 为儿童/

当我搜索表单“测试”并按 Enter 时,它会将我发送到

http://www.domain.com/toys/search/?q=test

我应该用什么替换发送给我的代码:

http://www.domain.com/toys/search/test

提前致谢!

【问题讨论】:

    标签: php forms search


    【解决方案1】:

    如果您使用引导程序,我假设您使用 JQuery。

    请按照以下步骤操作:

    HTML:

    <form class="navbar-form" role="search" method="get" action="<?=$domain?>search" id="searchFrom">
        <div class="input-group">
            <input type="text" id="searchField" class="form-control input" placeholder="Search" name="q">
            <div class="input-group-btn">
                <button  type="submit" class="btn btn-primary"><i class="glyphicon glyphicon-search"></i></button>
            </div>
        </div>
    </form>
    

    JS:

    $('body').on('submit','#searchFrom', function(e) {
        e.preventDefault();
        location.href = $(this).attr('action')+'/'+encodeURIComponent($('#searchField').val());
    });
    

    【讨论】:

    • 谢谢Thomas,说实话我不知道把你发给我的JS代码放在哪里。
    • 我看到我的主题使用了来自
    • 搜索字段值应通过encodeURIComponent 输入以避免意外结果(例如,如果搜索字符串包含?&amp;)。
    • 我尝试编辑答案,但编辑被拒绝。只需将上面包含location.href=… 的行替换为location.href = $(this).attr('action')+'/'+encodeURIComponent($('#searchField').val());
    • 编辑了编码的答案!要添加 JS 代码,请搜索加载 JS 文件的代码,并在其中添加一个带有 JS 代码的新文件!
    猜你喜欢
    • 2012-08-26
    • 1970-01-01
    • 2012-01-23
    • 2014-01-07
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多