无法通过后期响应更新Bootstrap的预输入数据源 [英] Trouble updating Bootstrap's typeahead data-source with post response

查看:73
本文介绍了无法通过后期响应更新Bootstrap的预输入数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Bootstrap的typeahead javascript插件,尝试通过jQuery的$ .post方法更改data-source属性.最初,我有:

Using Bootstrap's typeahead javascript plugin, I'm attempting to change the data-source attribute via jQuery's $.post method. Initially, I have:

<input type="text" data-provide="typeahead" data-source="["Option 1","Option 2","Option 3"]">

然后,假设单击了一个按钮,它将尝试更新数据源:

Then, let's say a button is clicked and it tries to update the data-source:

 $('button').on('click',function(){
     $.post('update.php',function(resp){
          $('input').attr('data-source',resp);
     });
  });

分别的XHR结果返回一个这样的数组:

The resp XHR result returns an array like this:

  ["One Option","Two Option","Three Option"]

我发现这不能用响应中构造的新数组可靠地更新数据源.

I'm finding that this does not reliably update the data-source with a new array that was constructed in the response.

有人知道这个问题可能是什么吗?

Does anyone know what the problem could be?

这似乎没有捕获所选值.有人知道如何通过Bootstrap使用typeahead获得所选值吗?

This does not appear to capture the selected value. Does anyone know how to get the selected value using typeahead with Bootstrap?

推荐答案

我最终想出了解决方法.它是在github此处概述的.

I eventually figured out how to do this. It is outlined on github here.

访问typeahead输入的data属性并直接修改源数组.例如:

Access the typeahead input's data attribute and modify the source array directly. E.g:

var autocomplete = $('input').typeahead();

//where newSource is your own array
autocomplete.data('typeahead').source = newSource; 

这篇关于无法通过后期响应更新Bootstrap的预输入数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆