如何获取在选择2输入字段上输入的当前输入值 [英] How to get the current input value which is entering on select 2 input field

查看:97
本文介绍了如何获取在选择2输入字段上输入的当前输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我第一次输入多重选择值时,我根据jquery中的以下代码获得了当前输入的文本值

When i'm first time entering multi select values i got the current entered text value based on below code in jquery

 <select class="form-control select2" multiple="multiple" data-placeholder="Select areas" style="width: 100%;" id="edit_areas" name="area">
 </select>
$(".select2-search__field").val()

$(document).on('keyup',".select2", function (e) {

    var city = $("#add_city option:selected").text();
    var location = $(".select2-search__field").val();});

效果很好.

但是在编辑中,当尝试输入更多值时,使用上述代码无法显示当前输入值

But on edit when try to enter more values,current entering value is failed to get by using above code its showing empty

注意:这里我是根据用户输入动态设置下拉菜单

Note: Here i'm setting the dropdown dynamically based on user enters

任何人都可以帮助我解决此问题.

Can anyone help me to resolve this issue.

推荐答案

一旦转换为select2,则应使用select2特定方法来获取和设置值.

You should use select2 specific methods to get and set the value, once you already converted to the select2.

设置值(例如,用于编辑)时,请使用以下代码:

When you are setting the value (e.g. for edit), use code like following :

$(".select2").select2("val", "Am,WY".split(",")); 

并使用以下代码获取值:

and to get the value, use code like following :

$(".select2").select2('val');

希望它可以解决您的问题(因为不确定您拥有的HTML和代码是什么,这只是一个猜测:-))

Hope it will resolve your issue (as not sure what is the HTML and code you are having, its just a guess :-))

这篇关于如何获取在选择2输入字段上输入的当前输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆