dongxixiu9134 2016-07-17 04:43
浏览 29
已采纳

如何删除选择2中的空白区域

i have dynamically add the value of the option in select2 it came from database but when the field have no value the space can be selected how should i remove the spaces? i tried this codes but nothing happens

 $imei=$row["IMEI_MX"];
   $imeiserial = explode(',', $imei);
    <select id="tags" name="imei" disabled class="form-control" onchange="getCount()" multiple>

    foreach($imeiserial as $imeiserial){
    $wew=ltrim($imeiserial, " \t.");

        echo '
        <option  value='.$imei.'>'.$wew.'</option>';

    [

]1

  • 写回答

1条回答 默认 最新

  • dongtun2572 2016-07-17 05:16
    关注

    I'm not entirely sure what your general layout is, but if you're using PHP to generate the HTML select tag, then you can try something like this:

    $imei = $row["IMEI_MX"];
    $imeiserial = explode(',', $imei);
    
    echo '<select id="tags" name="imei" disabled class="form-control" onchange="getCount()" multiple>';
    
    foreach($imeiserial as $is){
        $wew = trim($is);
    
        if (!empty($wew)) {
            echo "<option value='$wew'>$wew</option>";
        }
    }
    echo '</select>';
    

    The PHP empty() function will return true if the variable is NULL, an empty string, or does not exist. This means that if the value is a space (or empty string once the trim() function has gone through, then it won't output a html <option> tag.

    This is all assuming that you're meant to be creating a separate <option> for each element in the array after an explode (I'm guessing it's a comma-separated list of IMEIs) but you were outputting the entire string as the value rather than just the element.

    e.g.

    <option value='867637026628082, 867637026628090'>867637026628082</option>
    

    rather than

    <option value='867637026628082'>867637026628082</option>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000