javascript – 如何在Select2中使用“标签”

前端之家收集整理的这篇文章主要介绍了javascript – 如何在Select2中使用“标签”前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有这个“Select2”下拉菜单,从ajax和PHP填充.
我在这里的脚本有一个选择,并传递给一个html元素.现在我想将此代码与“标签”一起使用.我已经尝试但无法弄清楚如何获取所有选定的值?我如何发送所选的最新消息?

HTML

//form
<input type='hidden' class='col-md-4' id='choose_usr_email' name='choose_usr_email' required>
//Snap up whats chosed
<input type='text' id='chosen_usr_email' name='chosen_usr_email'>

使用Javascript

$(document).ready(function(){
var chosenUsr = $('#choose_usr_email');
$("#choose_usr_email").select2({
    tags: true,placeholder: "Välj användare..",ajax: {
    url: "time.PHP",dataType: 'json',//search term
        data: function (term,page) {
            return {
            q: term,// search term
            page: page
            };
        },results: function (data,page) {
        return { results: data};
        }
    } // Ajax Call
}); // Select2

// Start Change
$(chosenUsr).change(function() {
    var usrId = $(chosenUsr).select2('data').id;
    var usrEmail = $(chosenUsr).select2('data').text;
    var timeNr = $(chosenUsr).select2('data').timenr;
    var usrfName = $(chosenUsr).select2('data').usrfname;

    $('#chosen_usr_id').val(usrId);
    $('#chosen_usr_email').val(usrEmail);
    $('#chosen_usr_time_nr').val(timeNr);
    $('#chosen_usr_fname').val(usrfName);

  }); //Change
}); //Document Ready

解决方法

使用 select2 v.4.0,您可以使用多个下拉列表.

将name设置为choose_usr_email [],以便在提交时创建标记数组.

HTML

<form action="" id="tagForm">
    <select multiple="true" name="choose_usr_email[]" id="choose_usr_email" class="form-control select2">
        <!-- if tags are loaded over AJAX,no need for <option> elments -->
    </select>
    <!-- more form elements ... -->
    <button type="submit">Submit</button>
</form>

脚本

$('#choose_usr_email').select2({
    tags: true,// automatically creates tag when user hit space or comma:
    tokenSeparators: [","," "],ajax: {
        url: "time.PHP",page) {
            return {
                q: term,// search term
                page: page
            };
        },page) {
            return { results: data};
        }
    }
});

// handle form submission:
$('#tagForm').submit(function(e){
    e.preventDefault();
    $.ajax({
        // PHP file where you send selected values:
        url      : "file.PHP",// if you want to use $_POST in PHP,uncomment the line below:
        // type  : 'POST',dataType : 'json',// serialize the form:
        data     : $('#tagForm').serialize(),success  : function(response){
            // handle server response ...
        }
    });
});

PHP(发送所选值的文件)

<?PHP

    // If 'type' is not specified in AJAX,use $_GET
    // check if 'choose_usr_email' exists in AJAX request
    if(isset($_GET['choose_usr_email']){
        // if exists,loop through the values:
        foreach($_GET['choose_usr_email'] as $key => $value){
            // do something with each $value (each submitted tag)
        }
    }

?>

DEMO

对于Select2< V.4.0 $( ‘#choose_usr_email’)VAL();返回每个选定的标签ID(如果指定)或文本,以逗号(1,2,3,…)分隔.

$('#tagForm').submit(function(e){
    e.preventDefault();
    $.ajax({
        // PHP file where you send selected values:
        url      : "file.PHP",// request data,split input field value by comma:
        data     : {
            choose_usr_email : $('#choose_usr_email').val().split(',')
        },success  : function(response){
            // handle server response ...
        }
    });
});

然后,您可以像以前的PHP示例一样处理AJAX请求.

原文链接:https://www.f2er.com/js/155712.html

猜你在找的JavaScript相关文章