简体   繁体   English

如何记录在select2输入字段上键入的内容并最终更新另一个输入字段?

[英]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.

相关问题 javascript绑定另一个select2输入字段rails的select2输入onchange事件的数据 - javascript bind data of select2 input onchange event of another select2 input field rails 如何防止在数字输入字段中键入零或负数 - How to prevent a zero or negative number being typed in a number input field 填充select2输入字段中的项目 - Populating items in select2 input field Select2 从输入字段而不是下拉列表开始 - Select2 start with input field instead of dropdown 输入到输入字段的文本被复制到另一个输入字段的值的状态 - 为什么? - Text entered into an input-field is being duplicated to the state of another input field's value - why? ReactJs setState更新与输入框中输入的内容不同步 - ReactJs setState updating not in sync with what is being typed in the input box 根据其他输入字段从json中选择2 - select2 from json based on other input field 选择输入字段选项被添加到选择字段之外 - Select input field options being added outside select field 如何在更新另一个输入字段时更改输入字段的值? [HTML/JS] - How do i change the value of input field as another input field is being updated? [HTML / JS] 单击jquery中的输入字段时如何检测select2是否已加载? - how to detect if select2 has been loaded upon clicking the input field in jquery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM