2016-06-17 67 views
0

在php中使用開關,我想基於從數據庫中預先選擇的值來隱藏組合框的選項。我已經告訴過一個PHP switch語句來做到這一點,這裏就是我的了:是不可能用php隱藏組合框的選項?

<tr> 
       <td> 
       <select name="cbostatus" id="cbostatus"> 
       <option selected "" ></option> 
       <option value="1" <?php if ($row['task_status']=='Planned') echo 'selected="selected"';?>>Planned</option> 
       <option value="2" <?php if ($row['task_status']=='Started') echo 'selected="selected"';?>>Started</option> 
       <option value="3" <?php if ($row['task_status']=='Available') echo 'selected="selected"';?>>Available</option> 
       <option value="4" <?php if ($row['task_status']=='Impeded') echo 'selected="selected"';?>>Impeded</option> 
       <option value="5" <?php if ($row['task_status']=='Concluded') echo 'selected="selected"';?>>Finished</option> 
       </select> 
       </td> 
       </tr> 

這是組合,和這裏的開關:

   <?php 
       switch ($row['task_status']) 
       {    
        case 'Planned': 
         echo ' 
          <script> 
            cbostatus.options[4].style.display = "none"; 
            cbostatus.options[5].style.display = "none"; 
            cbostatus.options[3].style.display = "none"; 
          </script> 
         '; 
         break; 

        case 'Available': 
         echo ' 
          <script> 
            cbostatus.options[1].style.display = "none"; 
            cbostatus.options[4].style.display = "none"; 
            cbostatus.options[5].style.display = "none"; 
          </script> 
         '; 
         break; 

        case 'Started': 
         echo ' 
          <script> 
            cbostatus.options[1].style.display = "none"; 
          </script> 
         '; 
         break; 

        case 'Impeded': 
         echo ' 
          <script> 
            cbostatus.options[1].style.display = "none"; 
          </script> 
         '; 
         break; 

        case 'Concluded': 
         echo ' 
          <script> 
            cbostatus.options[1].style.display = "none"; 
          </script> 
         '; 
         break; 

       } 
       ?> 

但不工作。爲什麼?這是不可能的嗎?我一直試圖這樣做三天了。任何人有任何想法?

+5

哪裏/這個JS如何運行?如果你在實際的表單元素之前把它放到頁面中,那麼js將找不到任何東西,因爲那部分頁面還沒有被解析。 –

+2

哦,上帝,非常感謝你,你不知道我多麼感激,謝謝你 – RazorFinger

+0

你以前分配過'cbostatus'嗎? (用'document.getElementById' f.e.) – Jeff

回答

2

我不認爲交換機是實現它的最好方法,我會放棄JavaScript並使用PHP來完成這一切。這樣你就不必擔心隱藏選項,因爲只有你需要的選項纔會在那裏開始。我的建議是這樣,而不是:

定義每個狀態數值和可見選項的數組:

$status = [ 
    'Planned' => ['val' => 1, 'options' => ['Planned','Started']], 
    'Started' => ['val' => 2, 'options' => ['Started','Available','Impeded','Concluded']], 
    'Available' => ['val' => 3, 'options' => ['Started','Available']], 
    'Impeded' => ['val' => 4, 'options' => ['Started','Available','Impeded','Concluded']], 
    'Concluded' => ['val' => 5, 'options' => ['Started','Available','Impeded','Concluded']], 
]; 

然後輸出您的選擇基於任務的狀態這樣的選擇:

<tr> 
    <td> 
     <select name="cbostatus" id="cbostatus"> 
     <option selected "" ></option> 
     <?php foreach ($status[$row['task_status']]['options'] as $option) { 
      $selected = $row['task_status'] == $option ? 'selected="selected"' : ''; 
      echo '<option value="'. $status[$option]['val'] .'">'. $option .'</option>'; 
     } ?> 
     </select> 
    </td> 
</tr>