我如何获得multiselect tokenize2控件的文本

时间:2017-07-11 14:18:34

标签: jquery tokenize2

如何获取多选tokenize2控件的文本? 我可以轻松获得多选的价值,但我不知道如何获取文本

插件链接: https://zellerda.github.io/Tokenize2/index.html

代码:

<select class="tokenize-demo" multiple>
  <option value="1">Africa</option>
  <option value="2">Americas</option>
  <option value="3">Asia</option>
  <option value="4">Europe</option>
  <option value="5">Oceania</option>
</select>

Jquery的:

<script>
  $('.tokenize-demo').tokenize2();
    $('.tokenize-demo').on("tokenize:tokens:add", function (event, value) {
    console.log("Value is "+value); //here i am getting selected value like 1,2,3..etc.
    console.log("Text is "+?);//here i want text from options like Africa
  });
</script>

1 个答案:

答案 0 :(得分:2)

没有足够的文档,但我设法找到了解决方案。 Text属性实际上是在事件侦听器函数的第三个参数中传递的。修改您的事件监听器,如下所示:

$('.tokenize-demo').on("tokenize:tokens:add", function (event, value, text){
     console.log("Value is "+value); // To get value
     console.log("Text is "+ text); // To get text
});

供参考,请参阅tokenize:tokens:add event documentation