【问题标题】:search with enter key用回车键搜索
【发布时间】:2021-02-20 12:05:41
【问题描述】:

我有一个用于我的网站的房地产插件。此插件的搜索工具无法使用键盘上的回车键。我想在搜索工具中输入关键字后使用回车键进行搜索。我该怎么做?

我的网站:https://memoshome.com

我认为相关的代码是这样的:

$('.ere-advanced-search-btn', css_class_wrap).on('click', function (e) {
            e.preventDefault();
            var search_form = $(this).closest('.search-properties-form'),
                search_url = search_form.data('href'),
                search_field = [],
                query_string = '?';
            if (search_url.indexOf('?') !== -1) {
                query_string = '&';
            }
            $('.search-field', search_form).each(function () {
                var $this = $(this),
                    field_name = $this.attr('name'),
                    current_value = $this.val(),
                    default_value = $this.data('default-value');
                if (current_value != default_value) {
                    search_field[field_name] = current_value;
                }
            });
            $('.ere-sliderbar-filter', search_form).each(function () {
                var $this = $(this),
                    field_name_min = $this.find('.min-input-request').attr('name'),
                    field_name_max = $this.find('.max-input-request').attr('name'),
                    current_value_min = $this.find('.min-input-request').val(),
                    current_value_max = $this.find('.max-input-request').val(),
                    default_value_min = $this.data('min-default'),
                    default_value_max = $this.data('max-default');
                if (current_value_min != default_value_min || current_value_max != default_value_max) {
                    search_field[field_name_min] = current_value_min;
                    search_field[field_name_max] = current_value_max;
                }
            });
            var other_features = '';
            $('[name="other_features"]', search_form).each(function () {
                var $this = $(this),
                    value = $this.attr('value');
                if ($this.is(':checked')) {
                    other_features += value + ";";
                }
            });
            if (other_features !== '') {
                other_features = other_features.substring('0', other_features.length - 1);
                search_field['other_features'] = other_features;
            }
            if (search_field !== []) {
                for (var k in search_field) {
                    if (search_field.hasOwnProperty(k)) {
                        query_string += k + "=" + search_field[k] + "&";
                    }
                }
            }
            query_string = query_string.substring('0', query_string.length - 1);
            window.location.href = search_url + query_string;
        });

【问题讨论】:

  • 我对输入键没有问题(在 chrome 上)
  • 另外搜索工具不在
    标签中
  • 如果我们没有显示问题的样本,将很难为您提供更多帮助...

标签: jquery search enter


【解决方案1】:

我认为如果您将其添加到您的 btn type="submit" 中,将 'click' 替换为 'submit' 可能会起作用

【讨论】:

  • 我以前试过这个。搜索工具不在
    标记中。可以吗。
【解决方案2】:

没有html代码,你可以试试:

$('.my-search-box').keydown((ev) => {//i suppose .my-search-box' is class of input search
  if (ev.which == 13 ){//enter   
     $('give the id/class of button search').trigger("click");
  }
});

如果在输入搜索框中输入了事件,您会陷入陷阱,因此您会触发点击按钮搜索

【讨论】:

  • @devo-zara 很高兴帮助您不要忘记验证和投票的答案。这将关闭问题,就像 stakoverflow 运行
  • 我注意到另一个问题。当我更改输入值时,它无法检测到。例如:memoshome.com/en/advanced-search/… 如果我更改显示 itemtitle 的位置并按 Enter,则值不会更改。但如果我用鼠标按下搜索按钮,它就会改变。
  • @devo-zara 很难在真实的网站上进行测试,您必须在 sn-p.functional 上重现相同的问题..所以我看不到问题.....
【解决方案3】:

在您的搜索输入文本框中,试试这个:

$('.my-search-box').keypress(function (e) {
 var key = e.which;
 if(key == 13)  // the enter key code
  {
    $('.ere-advanced-search-btn', css_class_wrap).click();
    return;  
  }
});   

【讨论】:

  • 很遗憾没有。
猜你喜欢
  • 2018-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-17
相关资源
最近更新 更多