dpleylxzx47207117 2014-02-26 16:31
浏览 61
已采纳

使用选择的Jquery从php预填充多选框

I am using this script for my dropdowns http://harvesthq.github.io/chosen/

This is my select option which works great when starting with a blank field.

<form>
    <select class="chosen-select" multiple="multiple" id="fm-site[]" name="fm-site[]" style="width:250px" value="<?php echo $user_site;?>">
                    <option value="Pokerstars" <?php if(in_array("Pokerstars",$site_array)){echo 'selected';}?>>Pokerstars</option>
                    <option value="Full Tilt Poker" <?php if(in_array("Full Tilt Poker",$site_array)){echo 'selected';}?>>Full Tilt Poker</option>
                    <option value="Party Poker" <?php if(in_array("Party Poker",$site_array)){echo 'selected';}?>>Party Poker</option>
                    <option value="Ipoker" <?php if(in_array("Ipoker",$site_array)){echo 'selected';}?>>Ipoker Network</option>
                    <option value="Merge" <?php if(in_array("Merge",$site_array)){echo 'selected';}?>>Merge</option>
                    <option value="Winners Poker Network" <?php if(in_array("Winners Poker Network",$site_array)){echo 'selected';}?>>Winners Poker Network</option>
                    <option value="Microgaming" <?php if(in_array("Microgaming",$site_array)){echo 'selected';}?>>Microgaming</option>
                    <option value="Ongame" <?php if(in_array("Ongame",$site_array)){echo 'selected';}?>>Ongame</option>
                    <option value="Pacific" <?php if(in_array("Pacific",$site_array)){echo 'selected';}?>>Pacific</option>
                    <option value="Boss" <?php if(in_array("Boss",$site_array)){echo 'selected';}?>>Boss Poker</option>
                    <option value="PKR" <?php if(in_array("PKR",$site_array)){echo 'selected';}?>>PKR Network</option>
                    <option value="Winning Poker" <?php if(in_array("Winning Poker",$site_array)){echo 'selected';}?>>Winning Poker Network</option>
                    <option value="Revolution/Cake" <?php if(in_array("Revolution/Cake",$site_array)){echo 'selected';}?>>Revolution/Cake</option>
                    <option value="Winimax" <?php if(in_array("Winimax",$site_array)){echo 'selected';}?>>Winimax</option>
                </select>
</form>

To allow users to edit it i pre-populate the current info from the database, this is working fine on all the normal input fields but cant get this working for the multiple select.

I have all the values in an array like this:

$site_array = ("Pokerstars","Full Tilt Poker", "Party Poker", "Ipoker");

But when the form is printed out it only does the selected on the first one but in my debugger i can clearly see the array has Full Tilt Poker etc so i am not sure why it wont work. I also tried selected="selected" but i have a feeling that the chosen script is overwriting this because looking at the html the way the chosen Jquery works to add the selected is like this:

<div id="fm_site___chosen" title="" style="width: 250px;" class="chosen-container chosen-container-multi">
    <ul class="chosen-choices">
        <li class="search-choice">
            <span>Pokerstars</span>
            <a data-option-array-index="0" class="search-choice-close"></a>
        </li>
        <li class="search-choice">
             <span>Full Tilt Poker</span>
             <a data-option-array-index="1" class="search-choice-close"></a>
       </li>
       <li class="search-field">
           <input value="Select Some Options" class="" autocomplete="off" style="width: 25px;" type="text">
       </li>
   </ul>

   <div class="chosen-drop">
   <ul class="chosen-results">
       <li data-option-array-index="0" style="" class="result-selected">
           Pokerstars
       </li>
       <li data-option-array-index="1" style="" class="">
           Full Tilt Poker
       </li>
       <li data-option-array-index="2" style="" class="active-result">
           Party Poker
       </li>
       <li data-option-array-index="3" style="" class="active-result">
           Ipoker Network
       </li>
       <li data-option-array-index="4" style="" class="active-result">
           Merge
       </li>
       <li data-option-array-index="5" style="" class="active-result">
           Winners Poker Network
       </li>
       <li data-option-array-index="6" style="" class="active-result">
           Microgaming
       </li>
   </ul>
   </div>
</div>

And i notice when a value gets added in this format that it doesnt update the selected value inside the option value and if you manually add it then it sometimes overwrites it. So i wonder is there a way via chosen jquery to pass it which ones should be pre-selected or maybe there is another similar jquery someone can recommend that does this, i have red the documentation but i havent seen anything specific to this issue but surely its possible?

  • 写回答

1条回答 默认 最新

  • douchendan0040 2014-02-26 17:51
    关注

    This turned out to be correct, the problem was there was an invisible space before each value. i just added this line of code and it corrected it but its probably so isolated this wont help people so feel free to close

    I added this to the function that converted the string to an array

    $string_result=array_map('trim',$string_result);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码