我使用的是https://github.com/sliptree/bootstrap-tokenfield,注意到值是以字符串而不是数组的形式存储的。有没有办法将值存储为数组而不是字符串?
<input type="text" class="form-control" id="tokenfield" value='A,B,C' />https://jsfiddle.net/ds2xp4w1/1/
我还注意到value属性似乎没有更新。
发布于 2019-03-15 10:35:23
有两种方法
1.使用split(',')在dom中存储转换为数组的,分隔字符串
console.log($('#tokenfield').val().trim().split(','))<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" class="form-control" id="tokenfield" value='A,B,C' />
2.将字符串.convert到数组的JSON.stringify()格式
$('#tokenfield').val(JSON.stringify(['A','B','C']))
//after retrive
console.log(JSON.parse($('#tokenfield').val()))<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" class="form-control" id="tokenfield" value='' />
https://stackoverflow.com/questions/55174710
复制相似问题