无法同时在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();
答
使用end()
尝试 - http://jsfiddle.net/YRJLY/
var theText = "Printer Setup";
$("#sow option:contains(" + theText + ")")
.attr('selected', 'selected')
.end()
.find('#divoption5')
.show();
答
也许这是你想要做什么? (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;
}
}
会需要更多的代码 – j08691