这是我为选择脚本提供的代码:
var $select = $('#select-links').selectize({
theme: 'links',
maxItems: 1,
valueField: 'id',
searchField: 'title',
options: [
{id: 1, title: 'corp.blpnexi.com', preface: 'Primary/Std', value: 'https://corp.blpnexi.com' , secure: 'https://'},
{id: 2, title: 'alpha.vidyo.com', preface: 'Secondary/DR', value: 'https://alpha.vidyo.com' , secure: 'https://'},
],
render: {
option: function(data, escape) {
return '<div class="option">' +
'<span class="preface">' + escape(data.preface) + '</span>' +
'<span class="secureClass">' + escape(data.secure) + '</span>' +
'<span class="title">' + escape(data.title) + '</span>' +
'</div>';
},
item: function(data, escape) {
document.getElementById("submit-portal-select").value = data.value;
var portal = data.value;
$("#portal").val(portal).change();
localStorage.portal = portal;
console.log("data selected:" + data.title);
var classOf = 'titleI2';
if(data.title === "corp.blpnexi.com"){
classOf = 'titleI';
}
return '<div class="option">' +
'<span class="prefaceI">' + escape(data.preface) + '</span>' +
'<span class="divClass">'
+'<span class="secureClass">' + escape(data.secure) + '</span>' +
'<span class='+ classOf+'>' + escape(data.title) + '</span>' +
'</span>'+
'</div>';
}
},
create: function(input) {
return {
id: 0,
title: input,
preface: '#',
value: null,
secure: 'https://'
};
}
});
var selectize = $select[0].selectize;
selectize.setValue(1);
我设置了一些断点,并尝试调试。 当我选择一个值时,它进入项目功能。我将thelocalStorage.portal设置为该项目的dava.value。 这是第一次,但第二次我尝试使用它,它不会输入此代码,它不会更改,将我的localStorage保留为我首先设置的值。如何让它运行该代码?或者现在,如何在需要时获取正确的值?
答案 0 :(得分:0)
我为onChange添加了一个回调,并且这样做了:
onChange: function(value) {
console.log("data selected:" + value);
var portal;
if(value == 1){
portal = "https://corp.blpnexi.com";
}else{
portal = "https://alpha.vidyo.com";
}
document.getElementById("submit-portal-select").value = portal;
localStorage.portal = portal;
}