如何用js清除tokenfield
how to clear tokenfield with js
我想为 jquery 令牌字段制作一个重置按钮,但不知道如何重置它。
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 = "";
}
你就不能这样做吗:
$('#testtokenfield').tokenfield('destroy');
使用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>
<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 = " ";
})
we need to attach a click event to the clear button element and set a testtokenfield.value property to an empty string " ". so that when a user clicks on the button, the input field value is cleared.
我想为 jquery 令牌字段制作一个重置按钮,但不知道如何重置它。
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 = "";
}
你就不能这样做吗:
$('#testtokenfield').tokenfield('destroy');
使用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>
<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 = " ";
})
we need to attach a click event to the clear button element and set a testtokenfield.value property to an empty string " ". so that when a user clicks on the button, the input field value is cleared.