简体   繁体   English

Select2-使用多个select2作为文本框

[英]Select2 - Use multiple select2 as text box

I am using select2 ajax functionality to populate users email into select field. 我正在使用select2 ajax功能将用户电子邮件填充到选择字段中。 When there are no users email I should be able to enter email manually. 如果没有用户发送电子邮件,我应该可以手动输入电子邮件。

here is the code 这是代码

= f.collection_select :bcc_email_ids, Service::BccEmail.where("ticket_id = ?", @ticket.id), "email", :name_with_email, {selected: @ticket.bcc_emails.map(&:email)}, class: "form-control custom-select multiple_select_cc_dropdown", multiple: true

I am populating emails in select2 dropdown through ajax call given below: 我正在通过下面给出的ajax调用在select2下拉列表中填充电子邮件:

$.ajax({
        url: url,
        data: post_data,
        type: "get",
        dataType: 'json',
        format: "json",
        success: function(data){
          $.each(data.records, function(key, value) {
            arr.push({id: value.email, text: value.first_name + " " + value.last_name + "-" + value.email})
          })
          $(".multiple_select_cc_dropdown").html('').select2({data: arr });
        },
        error: function(data){

        }
      });

when there are no email populated then I should be able to enter email manually as textbox? 当没有电子邮件填充时,我应该能够手动输入电子邮件作为文本框吗?

Use tags: true inside select2 function. 使用标记:select2函数内部为true。

$.ajax({
        url: url,
        data: post_data,
        type: "get",
        dataType: 'json',
        format: "json",
        success: function(data){
          $.each(data.records, function(key, value) {
            arr.push({id: value.email, text: value.first_name + " " + value.last_name + "-" + value.email})
          })
          $(".multiple_select_cc_dropdown").html('').select2({tags: true,data: arr });
        },
        error: function(data){

        }
      });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM