1

我想为 jquery tokenfield 创建一个重置按钮,但不知道如何重置它。

HTML

<input type="text" class="input" name="testtokenfield" id="testtokenfield" placeholder="testtokenfield" value="">
<button onclick ="clear()"></button>

JS

$('#testtokenfield').tokenfield({
        showAutocompleteOnFocus: true
}); 
function clear(){
     document.getElementById('testtokenfield').value = "";
}
4

3 回答 3

0

你不能这样做:

$('#testtokenfield').tokenfield('destroy');

参考:Bootstrap 的 Tokenfield

于 2021-01-27T14:19:22.343 回答
0

使用setToken方法将空数组传递给您的输入

$('#testtokenfield').tokenfield('setTokens', []);

请参阅下面的工作片段:

$(function() {
  $('#testtokenfield').tokenfield({
    autocomplete: {
      source: ['red', 'blue', 'green', 'yellow', 'violet', 'brown', 'purple', 'black', 'white'],
      delay: 100
    },
    showAutocompleteOnFocus: true
  });

 $("#btn").on("click", function(e) {
 
  $('#testtokenfield').tokenfield('setTokens', []);
 });
});
body {
  padding: 5px;
}
<link href="http://code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
<script src="http://sliptree.github.io/bootstrap-tokenfield/docs-assets/js/typeahead.bundle.min.js"></script>
<script src="http://sliptree.github.io/bootstrap-tokenfield/dist/bootstrap-tokenfield.js"></script>
<script src="http://sliptree.github.io/bootstrap-tokenfield/dist/bootstrap-tokenfield.js"></script>
<script
  src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"
  integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU="
  crossorigin="anonymous"></script>
<link href="http://sliptree.github.io/bootstrap-tokenfield/dist/css/bootstrap-tokenfield.css" rel="stylesheet"/>
<link href="http://sliptree.github.io/bootstrap-tokenfield/dist/css/tokenfield-typeahead.css" rel="stylesheet"/>
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>




<input type="text" class="input" name="testtokenfield" id="testtokenfield" placeholder="" value="">
<br>
<button class="btn btn-success" id="btn">clear</button>

于 2021-01-27T14:47:45.373 回答
0
<input type="text" class="input" name="testtokenfield" id="testtokenfield" placeholder="testtokenfield"/>
<button id='clearbtn'>Clear</button>
    
const testtokenfield= document.getElementById("testtokenfield");        
const clearbtn= document.getElementById("clearbtn");

clearbtn.addEventListener('click', ()=>{
                // clearing the input field
                testtokenfield.value = " ";
            })

我们需要将点击事件附加到清除按钮元素并将 testtokenfield.value 属性设置为空字符串“”。这样当用户点击按钮时,输入字段的值就会被清除。

于 2021-01-27T17:46:53.037 回答