简体   繁体   English

使用PHP以select2为倍数设置所选值

[英]Set the selected value in select2 multiple using PHP

How will i set the selected item in select2 multiple ? 我将如何以select2为倍数设置所选项目? i would like to set these option/values as selected by default in a multiple select list and display them to the user where they will be able to updated their data if necessary. 我想将这些选项/值设置为默认情况下在多选列表中选择的值,并将其显示给用户,以便他们在必要时能够更新其数据。

<?php
$mytitle  = array(
"867310020292434, 867310021548131, 867310021561670");
$test = implode(',', $mytitle);

$query=mysqli_query($link,"select * from inventory where ITEM_CODE_MX='OPP01-3006GRY'");
while($row=mysqli_fetch_array($query))
{

$imei=$row["IMEI_MX"];
$title = explode(',', $imei);
}
?>
<script>

  $(function () {
    $('#tags').select2();
    function select (event){
      $("#tags").select2({
        maximumSelectionLength: $('#quantitytotransfer').val(),
        formatSelectionTooBig: function (limit) {
          $('#box').show().text('Callback!');
          parseInt($("#quantitytotransfer").val())
          return 'Too many selected elements (' + limit + ')';
        }
      });
    }

    $('#quantitytotransfer').on('keyup', select);

    select()
  });

</script>


<select id="tags" name='title[]' style="width:300px;"  class="form-control select2-offscreen" onchange="getCount()" multiple>
<?php
  foreach ($title as $opt) {
      $sel = '';
    $wew = trim($is);
      if (in_array($opt, $mytitle)) {

      $sel = 'selected="selected" ';
      }
     if (!empty($opt)) {
      echo "<option ' . $sel . ' value='$opt'>$opt</option>";
    }
  }
?>

</select>

if you want to select the first option here is the code 如果要选择第一个选项,则代码如下

$sel = "selected";
    foreach ($title as $opt) {
         if (!empty($opt)) {
            echo "<option value='" . $opt . "'" . $sel . ">" . $opt . "</option>";
            $sel = "";
        }

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

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