简体   繁体   English

如何禁用Bootstrap的预输入数据源?

[英]How to disable Bootstrap's typeahead data source?

How to disable bootstap typahead data source when readonly 只读时如何禁用Bootsap Typhead数据源

<input type="text" data-provide="typeahead" data-source="["Option 1","Option 2","Option 3"]" id="test1" name="test1">
<a href="#" class="btn" id="readonly">Readonly</a>

Now i am using jquery to apply readonly but when we press back space it suggest the last element from data-source 现在我正在使用jquery来应用只读,但是当我们按退格键时,它建议数据源中的最后一个元素

$('#readonly').on('click', function (e) {
    $("#test1").prop('readonly', true);
})

If you want input element readolny. 如果要输入元素readolny。 then add class disabled 然后禁用添加类

$('#readonly').on('click', function (e) {
   $("#test1").prop('readonly', true).addClass("disabled");
})

If you don't care about maintaining the typeahead.js functionality while the input is disabled, you can destroy the typeahead like so: 如果您不希望在禁用输入的情况下维护typeahead.js功能,则可以销毁typeahead,如下所示:

$('#textbox').typeahead('destroy');

This will reset/remove any attributes/styles typeahead.js may have added. 这将重置/删除typeahead.js可能添加的任何属性/样式。 If later on you wanted to add back the typeahead.js functionality, you could reinitialize it with: 如果以后您想重新添加typeahead.js功能,则可以使用以下方法重新初始化它:

$('#textbox').typeahead({ /* configs */ });

Thank you guys By your suggestion and answers i got solutions 谢谢你们的建议和答案,我得到了解决方案

$('#readonly').on('click', function (e) {
   $("#test1").prop('readonly', true); 

   var typeahead = $("#test1").data('typeahead'); //Get Typehead Object
   if (typeahead) typeahead.source = '';
   else $("#test1").typeahead({source: ''});

})

//When readonly false; //当readonly为false时;

var list = ["Option 1","Option 2","Option 3"];
var typeahead = $("#test1").data('typeahead'); //Get Typehead Object
if (typeahead) typeahead.source = list;
else $("#test1").typeahead({source: list});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM