如果结果为空,则禁用提交按钮typeahead.js

时间:2014-09-22 10:07:24

标签: javascript jquery typeahead.js

如果找不到查询结果,我希望我的提交按钮被禁用。在这种情况下,表格不应提交。我正在使用typeahead.js。这是我正在使用的代码

$('.typeahead').typeahead(null, {
        name: 'suburb',
        displayKey: 'suburb_name',
        limit: 10,
        source: suburb.ttAdapter(),
        templates: {
            empty: [
                '<div class="empty-message">',
                'No results Found',
                '</div>'
                ].join('\n'),
            suggestion: function (s){return '<p>'+s.postal_code+', '+s.suburb_name+'</p>';}
        }
    });

2 个答案:

答案 0 :(得分:2)

当source返回空时,typeahead API上没有直接事件。只是一个想法,您可以通过使用typeahead:selected和autcompleted events选择typeahead中的值来初始禁用按钮并启用按钮。

自定义活动的文档为here

$('.typeahead').on('typeahead:selected', enableSubmit); 
$('.typeahead').on('typeahead:autocompleted', enableSubmit);


function enableSubmit(){

      //This will trigger when something is selected from typeahead
      //Enable the button here

}

答案 1 :(得分:1)

您可以查找您的.empty-message类,并在将元素插入DOM时触发jQuery。

$(document).on('DOMNodeInserted', function(e) {
    if ($(e.target).is('.empty-message')) {
       $("#myFormBtn").attr("disabled","disabled");
    }
});
相关问题