无法同时在jquery中同时使用这两个操作

问题描述:

因此,这是问题所在。我编写了一个基于所选下拉菜单打开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

使用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

也许这是你想要做什么? (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; 
     } 
}​