2014-09-23 49 views
-2

我想創建一個基於另一個下拉列表的選擇下拉列表。 如果選擇Brand PreferenceBrand Purchased,則應出現DIV select_a_brand。我使用下面的代碼。它隱藏了DIV select_a_brand,但是當我選擇一個選項Brand PreferenceBrand Purchased時,仍然隱藏DIV select_a_brand。沒有顯示。我該如何解決這個問題?如何使用JQuery創建條件下拉菜單?

<div class="row"> 
    <label class="form_controller required">By Brand</label> 
    <select style="width: 218px;"> 
     <option>Select an option</option> 
     <option id="brand_preference">Brand Preference</option> 
     <option id="brand_purchased">Brand Purchased</option> 
    </select> 
    <div id="select_a_brand"> 
     <?php echo $form->dropDownList($model,'brand_id', gGetBrandList('brand_id', 'brand_id, brand_name'), array('prompt'=>'Select a brand')); ?> 
    </div> 

    <script type="text/javascript"> 
     $(document).ready(function() { 
      jQuery('#select_a_brand').hide();     

      jQuery('#brand_preference').select(function(){ 
       jQuery('#select_a_brand').show();  
      });    
     }); 
    </script>   
</div> 
+1

'.select()'是選擇元素內容事件的處理程序。你正在尋找'.change()'。 – melancia 2014-09-23 05:17:29

+0

謝謝你。但它仍然沒有任何東西..小提琴在這裏:http://jsfiddle.net/riffaz/vyvjww79/2/ – 2014-09-23 05:46:02

+0

從問題中刪除'PHP'位,並將其替換爲生成的HTML標記。 – melancia 2014-09-23 05:53:17

回答

1

你應該適當地使用jQuery的change事件處理程序;另外,您必須綁定到父級選擇框。 IHAVE改變jsfiddle,現在運作良好:請參考下面的代碼:

$(document).ready(function() { 
    jQuery('#select_a_brand').hide();     

    jQuery("#base_option").change(function() {  


     if($(this).val() == "Brand Purchased"){ 
      alert("Handler for .change() called."); 
     } 
    }); 

}); 

選擇不是事件它只是API來選擇下拉列表中特定索引/值。

+0

謝謝你。但仍然沒有什麼..小提琴在這裏:http://jsfiddle.net/riffaz/vyvjww79/2/ – 2014-09-23 05:45:39

+0

重新編輯的問題,使其功能。 – 2014-09-23 10:13:13