【问题标题】:Disable button unless Selected from Typeahead Autocomplete除非从 Typeahead Autocomplete 中选择,否则禁用按钮
【发布时间】:2015-05-02 09:35:09
【问题描述】:

--- 编辑:解释流程---

在输入字段中,用户应该输入任何字符串,基本上是他们正在搜索的公司的名称。一旦用户输入了 3 个或更多字符,AJAX 调用就会进入数据库,并获取与用户查询匹配的结果,并将其显示为 Google 或浏览器 URL 栏中的搜索建议。用户从建议中选择一个项目,然后单击按钮,会触发一个名为 setCompany() 的函数,该函数执行不同的操作。

我想要的是,应该禁用根据搜索查询触发进一步操作的按钮,直到用户从 Bloodhound,Typeahead 提出的建议中选择一个项目。

--- 结束编辑 ---

我正在使用下面的代码使用户能够从 Typeahead、Bloodhound 生成的下拉列表中选择值。

$(document).ready(function() {
        //Set up "Bloodhound" Options       
                var my_Suggestion_class = new Bloodhound({
                    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('keyword'),
                    queryTokenizer: Bloodhound.tokenizers.whitespace,
                    remote: {
                        url: "{{ URL::to('user/company/%compquery') }}",
                        filter: function(x) {
                            return $.map(x, function(item) {
                                return {keyword: item['name']};
                            });
                        },
                        wildcard: "%compquery"
                    }
                });

                // Initialize Typeahead with Parameters
                my_Suggestion_class.initialize();
                var typeahead_elem = $('.typeahead');
                typeahead_elem.typeahead({
                    hint: false,
                    highlight: true,
                    minLength: 3
                },
                {
                    // `ttAdapter` wraps the suggestion engine in an adapter that
                    // is compatible with the typeahead jQuery plugin
                    name: 'results',
                    displayKey: 'keyword',
                    source: my_Suggestion_class.ttAdapter(),
                    templates: {
                          empty: 'No Results'
                        }
                });
            });

这是 HTML:

<div class="iner-sub-header" style="border-bottom: 1px solid #ccc;">
                <div class="row" style = "background-color: white;">
                    <div class="col-md-12 heading">
                        <div id = "results" class="col-md-12 col-xs-12 results">
                            <span class="glyphicon glyphicon-search span-search" aria-hidden="true"></span>
                            <input type="search" class="typeahead custom-input-padding" placeholder="Search Company" id="keyword" onselect="setCompany();">
                            <button class="btn go-btn" id="go" onclick="setCompany()">SEARCH</button>
                        </div>
                    </div>
                </div>
            </div>

我想确保触发 setCompany() 函数的按钮被禁用,直到用户从 Typeahead 生成的下拉列表中选择某些内容。

谁能帮帮我?

【问题讨论】:

  • 你能不能把代码弄一下,我只是从代码上看不懂这个问题。
  • 将下拉列表的可能值存储在全局变量中,禁用页面加载或html上的按钮,添加$(document).on('change',[dropdown_selector], function(){logic to enable the button}),如果值在函数内部,则使用.removeAttr()重新启用按钮全局数组
  • 不能那样做。在某些情况下,结果集可能有数千个。

标签: javascript jquery html typeahead


【解决方案1】:

试试

html

<button class="btn go-btn" id="go" onclick="setCompany()" disabled="true">SEARCH</button>

js

 typeahead_elem.bind("typeahead:select", function(ev, suggestion) {
    $("#go").prop("disabled", false);
 });

disabledtypeahead.js - Custom Events

【讨论】:

  • 我试图完成这项工作,但没有成功。你能澄清一下我应该在函数(ev,建议)中写什么吗?我尝试了 function(ev, my_Suggestion_class) 但这没有用..
  • @Shahzeb 不确定是否完全解释问题?是否要求解决 a) js 的“建议”部分; b) 选择“建议”后的“禁用”、“启用”按钮; c)a)和b)?,问题似乎表明“建议”部分已经起作用;请参阅 OP “我正在使用下面的代码使用户能够从 Typeahead、Bloodhound 生成的下拉列表中选择值。” ?可以澄清一下吗? , 创建 stacksn-ps blog.stackoverflow.com/2014/09/… , jsfiddle jsfiddle.net 来演示?
  • @Shahzeb 注意,当前答案未解决问题中js 的“建议”部分。另请注意“建议”对象,其中包含不建议出现在 OP 中的内容?建议对象未从 remote -> filter 返回; templates 似乎没有为“下拉菜单”返回“建议”html 字符串。见stackoverflow.com/q/28253261/2801559
  • 建议部分工作正常。我只是希望我的“开始”按钮被禁用,直到用户从建议中选择了一些东西。感谢您的建议,现在可以使用它了:)
【解决方案2】:

这使它对我有用。现在它工作得很好。

$(document).ready(function() {
        //Set up "Bloodhound" Options       
                var my_Suggestion_class = new Bloodhound({
                    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('keyword'),
                    queryTokenizer: Bloodhound.tokenizers.whitespace,
                    remote: {
                        url: "{{ URL::to('user/company/%compquery') }}",
                        filter: function(x) {
                            return $.map(x, function(item) {
                                return {keyword: item['name']};
                            });
                        },
                        wildcard: "%compquery"
                    }
                });

                // Initialize Typeahead with Parameters
                my_Suggestion_class.initialize();
                var typeahead_elem = $('.typeahead');

                typeahead_elem.typeahead({
                    hint: false,
                    highlight: true,
                    minLength: 3
                },
                {
                    // `ttAdapter` wraps the suggestion engine in an adapter that
                    // is compatible with the typeahead jQuery plugin
                    name: 'results',
                    displayKey: 'keyword',
                    source: my_Suggestion_class.ttAdapter(),
                    templates: {
                          empty: 'No Results'
                        }
                }).on("typeahead:selected typeahead:autocompleted", function(ev, my_Suggestion_class) {
                    $("#go").prop("disabled", false);
                });
             });

【讨论】:

    【解决方案3】:

    如果按钮的文本框的内容发生更改并且不是预先输入的选定值,我想要一种方法来重新禁用按钮。在 typeahead:select 我存储显示文本。在每个按键上,我确保文本仍然与存储的变量匹配。否则我会再次禁用该按钮。

        this.searchbox = $('.js-patient-search');
    
        this.searchbox.typeahead({
          hint: true
        }, {
          source: Bloodhound,
          display: function(obj) {
            return obj.first_name + ' ' + obj.last_name;
          }
        });
    
        this.searchbox.on('typeahead:select', (function(_this) {
          return function(e, suggestion) {
            _this.displayName = _this.searchbox.val();
            return $('.js-add-encounter').prop('disabled', false);
          };
        })(this));
    
        return this.searchbox.keyup((function(_this) {
          return function(e) {
            if (_this.searchbox.val() !== _this.displayName) {
              return $('.js-add-encounter').prop('disabled', true);
            }
          };
        })(this));
    

    注意:这是已解析的咖啡脚本,但我认为应该很清楚发生了什么。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-02
      • 2020-04-11
      • 1970-01-01
      • 2013-04-15
      • 1970-01-01
      • 1970-01-01
      • 2015-11-01
      • 2012-09-23
      相关资源
      最近更新 更多