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

Jquery 我可以从typeahead.js字段中获取值吗?,jquery,typeahead.js,Jquery,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

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

这可能吗

我怎么做这个

这可能是新发行的吗?

试试看

$(".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)
});
JSFIDLE

请参见试一试

$(".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)
});
JSFIDLE


请参见

我的代码解决方案,这并不容易理解,但要感谢@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;
    }
});

我的代码解决方案并不容易理解,但多亏了@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;
    }
});

为什么你不能?你不能像这样得到它
$(你的选择器)。val()
AFAIK你只能得到捕获插件更改事件的值。我已经检查过了,但不起作用…为什么你不能?你不能像这样得到它
$(你的选择器)。val()
AFAIK您只能获取捕获插件更改事件的值。我已检查,但不起作用。。。