Javascript 为输入类型选择“设置”;“文本”;要素
我有以下HTML/Bootstrap元素:Javascript 为输入类型选择“设置”;“文本”;要素,javascript,jquery,selectize.js,Javascript,Jquery,Selectize.js,我有以下HTML/Bootstrap元素: <div class="form-group"> <label>Keyword:</label> <input type="text" class="form-control" id="keyword"> </div> 由于这些起始值可能是随机的,因此我无法找到解决方法。您可以在初始化时使用create或createOnBlur选项 以下是selectize.js create允许
<div class="form-group">
<label>Keyword:</label>
<input type="text" class="form-control" id="keyword">
</div>
由于这些起始值可能是随机的,因此我无法找到解决方法。您可以在初始化时使用
create
或createOnBlur
选项
以下是selectize.js
create
允许用户创建不在初始选项列表中的新项目
createOnBlur
如果为true,当用户退出该字段(在输入之外单击)时,将创建并选择一个新选项(如果启用了创建设置)我了解了如何执行此操作:
//enable selectize on input element and add a list of options
var $select = $('#keyword').selectize({
maxItems: 1,
valueField: 'id',
labelField: 'title',
searchField: 'title',
options: [
{id: 1, title: 'value1'},
{id: 2, title: 'value2'},
{id: 3, title: 'value3'}
],
create: true,
});
//store selectize object to variable
var selectize = $select[0].selectize;
//add new option and set the value to new option
selectize.addOption({id:4, title: 'value4'});
selectize.setValue(4, false);
现在,加载页面后的默认选项为“value4”,可以根据需要选择其他选项。您是否有特定问题?如果你问如何设置这个插件,那么所有的文档和示例代码都可以在你提供的链接中找到……输入必须是文本类型有什么原因吗?我已经这样做了,但不是用插件,而是用ajax php和mysql数据库。我相信它也能很好地工作。大多数示例都是针对表单元素的,但我希望它是。这是可能的还是我需要使用?
//enable selectize on input element and add a list of options
var $select = $('#keyword').selectize({
maxItems: 1,
valueField: 'id',
labelField: 'title',
searchField: 'title',
options: [
{id: 1, title: 'value1'},
{id: 2, title: 'value2'},
{id: 3, title: 'value3'}
],
create: true,
});
//store selectize object to variable
var selectize = $select[0].selectize;
//add new option and set the value to new option
selectize.addOption({id:4, title: 'value4'});
selectize.setValue(4, false);