我有一个子reddit数组,我想预填充到taginputs中,但是索引0处的元素不断被跳过。我不确定为什么。
$.each(reddits, function(index, value) {
$('#Tags').tagsinput('add', value);
console.log(index);
});
以上是负责重新填充taginputs(https://imgur.com/a/vjw2rMm)的代码,但跳过了我的索引零元素。尽管在控制台上,它的确打印0:/
var reddits = [];
var exists = localStorage.getItem('redditList');
if(exists) {
reddits = JSON.parse( exists );
}
$.each(reddits, function(index, value) {
$('#Tags').tagsinput('add', value);
console.log(index);
});
$('#Tags').tagsinput({
confirmKeys: [13, 32]
});
$('input').on('itemAdded', function(event) {
var sub = String(event.item);
sub = sub.replace(/\s/g, '');
reddits.push(sub);
console.log(reddits);
saveList();
});
$('input').on('itemRemoved', function(event) {
reddits.splice(reddits.indexOf(event.item), 1);
console.log(reddits);
saveList();
});
function saveList() {
localStorage.setItem('redditList', JSON.stringify(reddits)); // store it as string
reddits = JSON.parse( localStorage.getItem('redditList')); // convert it to object
}
css
.inputContainer {
width: 250px;
}
.bootstrap-tagsinput {
background-color: #fff;
border: 1px solid #ccc;
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
display: inline-block;
padding: 4px 6px;
color: #555;
vertical-align: middle;
border-radius: 4px;
width: 95%;
line-height: 22px;
cursor: text;
}
.bootstrap-tagsinput input {
border: none;
box-shadow: none;
outline: none;
background-color: transparent;
padding: 0 6px;
margin: 0;
width: auto;
max-width: inherit;
}
.bootstrap-tagsinput.form-control input::-moz-placeholder {
color: #777;
opacity: 1;
}
.bootstrap-tagsinput.form-control input:-ms-input-placeholder {
color: #777;
}
.bootstrap-tagsinput.form-control input::-webkit-input-placeholder {
color: #777;
}
.bootstrap-tagsinput input:focus {
border: none;
box-shadow: none;
}
.bootstrap-tagsinput .tag {
background-color: #0abab5;
display: inline;
margin-right: 2px;
padding: 0 5px;
border-radius: 5px;
color: black;
}
.bootstrap-tagsinput .tag [data-role="remove"] {
margin-left: 8px;
cursor: pointer;
}
.bootstrap-tagsinput .tag [data-role="remove"]:after {
content: "x";
padding: 0px 2px;
}
.bootstrap-tagsinput .tag [data-role="remove"]:hover {
box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
}
.bootstrap-tagsinput .tag [data-role="remove"]:hover:active {
box-shadow: inset 0 3px 5px rgba(0, 0, 0, 0.125);
}
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Simple Chrome Extension</title>
<link rel="stylesheet" type="text/css" media="screen" href="css/bootstrap-tagsinput.css">
</head>
<body>
<div class="inputContainer">
<input type="text" id="Tags" class="form-control"/>
</div>
<script src="jquery-3.3.1.min.js"></script>
<script src="bootstrap-tagsinput.js"></script>
<script type="text/javascript" src="js/popup.js"></script>
</body>
</html>