2012-06-15 48 views
0

因此,這是問題所在。我編寫了一個基於所選下拉菜單打開div的代碼。這很好。然後我想要預先選擇一個特定的選項($ _POST ['value'])。這很好。問題出現在我還想讓相關div打開的時候。我可以做一個或另一個,但從來沒有在一起。請注意,我包含了工作選項並對其進行了評論。請告訴我什麼是我應該做的事:無法同時在jquery中同時使用這兩個操作

$(document).ready(function(){ 
    $('.box').hide(); 
    $('#sow').change(function() { 
     $('.box').hide(); 
     $('#div' + $(this).val()).show(); 
    }); 
    }); 
    var theText = "Printer Setup"; 
    //$("#sow option:contains(" + theText + ")").attr('selected', 'selected'); 
    //$("#sow option:contains(" + theText + ")")$('#divoption5').show(); 
    $("#sow option:contains(" + theText + ")").attr('selected', 'selected')$('#divoption5').show(); 
+0

會需要更多的代碼 – j08691

回答

0

使用end()嘗試 - http://jsfiddle.net/YRJLY/

var theText = "Printer Setup"; 

$("#sow option:contains(" + theText + ")") 
    .attr('selected', 'selected') 
    .end() 
    .find('#divoption5') 
    .show(); 
+0

這也不能工作 – Refiking

+0

你是什麼意思它的工作中的jsfiddle或者你想不服別人 –

+0

IDK爲什麼它的工作。?。?在JSFiddle中,但不會我自己的代碼 – Refiking

0

也許這是你想要做什麼? (http://jsfiddle.net/ft2PD/13/顯示時所選擇的項目是需要顯示的DIV的一個,http://jsfiddle.net/ft2PD/14/顯示了當所選項目應隱藏的div

這裏是代碼:

<select id='myselect'> 
    <option selected='selected' value='-1'>Please Select </option> 
    <option value='0'>value 0</option> 
    <option value='1'>value 1</option> 
</select> 
<div id='content' style="display:none;"> 
    content will come here. 
</div> 

和JavaScript:

$(document).ready(function(){ 
    ShowDiv($('#myselect option:selected')); 
    $('#myselect').change(function(){ 

     ShowDiv(this); 
    }); 

}); 
function ShowDiv(item) 
{ 
    switch($(item).val()) 
     { 
      case "0": 
       $('#content').show(); 
       break; 
      default: 
       $('#content').hide(); 
       break; 
     } 
}​ 
相關問題