我可以从typeahead.js字段中获取值吗



我使用typeahead.js作为自动完成字段。无论是否在自动完成列表中,我都需要获取输入中的值。

这可能吗?

我该怎么做?

这会是一个新问题吗?

尝试

$(".typeahead").typeahead({/* typeahead options */})
.on("input", function(e) {
  // do stuff with current `typeahead` `value`
  var myVal = e.target.value; // `$(e.target).typeahead("val")
  console.log(myVal)
});

jsfiddlehttp://jsfiddle.net/2reurafk/1/

参见jQuery#typeahead('val')

我的代码解决方案并不容易理解,但多亏了@guest271314

$('form#prefetch').bind("keypress", function(e) {
    if (e.keyCode == 13) {
        e.preventDefault();
        var query = $(e.target).typeahead("val").split(" ").join("-").toLowerCase();
        return window.location = baseurl+'search/'+query;
    }
});

最新更新