Jquery Select2 Ajax+Tags

Jquery Select2 Ajax+Tags

本文关键字:Ajax+Tags Select2 Jquery      更新时间:2023-09-26
$("#nomlabo").select2({
    //tags:true,
    ajax: {
        dataType: "json",
        url: "includes/labotags.php",
        results: function (data) {
            //alert(data[1]);
            return {
                results: data
            };
        }
    },
    tags: ["result1", "result2", "result3", "result4"],
    maximumSelectionSize: 1
});

嗨,我想将数据结果中的值放入"标签"中, 这怎么可能?

我已经更改了代码以从PHP而不是jQuery获取:

$("#nomlabo").select2({
            tags: [
            <?php
            $results =array();
                if ($stm = $mysqli->prepare("SELECT * FROM laboratoire")) {
                        $stm->execute();
                        $stm->store_result();
                        $stm->bind_result($id,$nom_labo,$id_nationalite);
                        while ($stm->fetch()) {
                            echo '"'.$nom_labo.'",';
                        }
                    }
            ?>
            ],
            maximumSelectionSize: 1
        });