php
  • jquery
  • html
  • 2016-02-28 131 views 0 likes 
    0

    我有一個模式窗體,其中有兩個選擇框,它們與current_models類相同。將動態optgroup項加載到html中選擇

    <div class="row">  
        <div class="col-md-6"> 
         <div class="form-group"> 
         <label class="control-label">Model</label> 
         <select class='form-control' id='left_model_id' name='left_model_id'> 
          <div id="left_model" class="current_models"></div> 
         </select> 
         </div> 
        </div> 
        <div class="col-md-6"> 
         <div class="form-group"> 
         <label class="control-label">Model</label> 
         <select class='form-control' id='right_model_id' name='right_model_id'> 
          <div id="right_model" class="current_models"></div> 
         </select> 
         </div> 
        </div> 
    </div> 
    

    每個選擇需要有它自己的id,但它們是加載同一個select選項成組這樣的:

    $('.current_models').load('ajax_get_models_man_group.php'); 
    

    這裏是我的ajax_get_models_man_group.php頁:

    <?php 
    
    require_once 'core/init.php'; 
    
    $user = new User(); 
    if(!$user->isLoggedIn()) { 
    Redirect::to('login.php'); 
    } 
    
    
    $models = DB::getInstance()->query("SELECT man.id AS man_id, man.man AS man, models.name AS model, models.id AS model_id FROM hearing_aid_models models LEFT JOIN hearing_aid_man man ON man.id = models.man_id WHERE models.currently_fitting = 'Y' AND company_id = '" . $user->data()->company_id . "' ORDER BY man ASC"); 
    
    
    if ($models->error()) { 
        echo 'Error occurred.'; 
    } else { 
    
    $currentGroup = null; 
    foreach($models->results() as $result) { 
        // start a new optgroup 
        if($currentGroup == null || $result->man_id != $currentGroup) { 
         // end the previous group 
         if($currentGroup != null) { 
          echo "</optgroup\n>"; 
         } 
    
         // start a new group 
         echo "<optgroup data-id='{$result->man_id}' label='{$result->man}'>\n"; 
    
         $currentGroup = $result->man_id; 
        } 
    
        echo "<option value='{$result->model_id}'>{$result->model}</option>\n"; 
    } 
    
    // end the last opt group 
    if($currentGroup != null) echo "</optgroup>\n"; 
    
        } 
    ?> 
    

    問題是值不會加載到選擇div s!

    這裏是我的AJAX的源代碼結果:

    <optgroup data-id='1' label='Audibel'> 
    <option value='95'>Start 7 Wireless</option> 
    <option value='96'>Start 7</option> 
    <option value='99'>A3i Platinum</option> 
    </optgroup> 
    <optgroup data-id='16' label='Phonak'> 
    <option value='98'>Naida Q70</option> 
    </optgroup> 
    <optgroup data-id='2' label='Starkey'> 
    <option value='100'>S Series i30</option> 
    <option value='81'>3 Series I 70</option> 
    <option value='82'>3 Series I 90</option> 
    </optgroup> 
    
    +0

    請將您的標記和重新發布ajax sult – Enrico

    +0

    我添加了ajax結果。我還能提供什麼幫助?謝謝。 –

    +0

    在我看來你是通過ajax在錯誤的div內加載的。我會發佈一個解釋的答案。 – Enrico

    回答

    1

    1)擺脫的div選擇標籤內

    <div class="row">  
        <div class="col-md-6"> 
         <div class="form-group"> 
         <label class="control-label">Model</label> 
         <select class='form-control current_models' id='left_model_id' name='left_model_id'> 
         </select> 
         </div> 
        </div> 
        <div class="col-md-6"> 
         <div class="form-group"> 
         <label class="control-label">Model</label> 
         <select class='form-control current_models' id='right_model_id' name='right_model_id'> 
         </select> 
         </div> 
        </div> 
    </div> 
    

    2)正確的DIV中加載AJAX

    $('.current_models').load('ajax_get_models_man_group.php'); 
    
    +1

    非常感謝。我忽略了這種可能性!出於某種原因,我認爲.load只能使用跨度或div。我現在正在學習jquery。我很感激你花時間幫助我。 –

    相關問題