[英]How to log what's being typed on the select2 input field and eventually updating another input field?
I'm having trouble logging in real time what's being typed on the select2 input field 我无法实时记录在select2输入字段上键入的内容
My goal is to update another input field while the user in typing on the select2 search box 我的目标是在用户在select2搜索框上键入内容时更新另一个输入字段
I tried many things but it's not working: nothing is appearing in my console 我尝试了很多事情,但是没有用:控制台中什么也没出现
$('.mySelect2').on("change",function(e){
console.log("mySelect2 text", e.target.value)
console.log("mySelect2 text $(this).val()", $(this).val())
// #location is my other field that I want to update
$("#location").text(e.target.value);
});
According to the documentation , you need to use different event: 根据文档 ,您需要使用不同的事件:
$('#mySelect2').on('select2:select', function (e) {
var data = e.params.data;
console.log(data);
});
Update: the following method allows to capture everything that the user types into the select2 input 更新:以下方法可以捕获用户输入到select2输入中的所有内容
// In your Javascript (external .js resource or <script> tag) $(document).ready(function() { var $select2 = $('.js-example-basic-single') $select2.select2(); $('body').on('input', '.select2-search__field', function() { console.log(this.value); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.4/css/select2.min.css" rel="stylesheet" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.4/js/select2.min.js"></script> <select class="js-example-basic-single" name="state"> <option value="AL">Alabama</option> ... <option value="WY">Wyoming</option> </select>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.