我正在开发一个php项目在线预约表格,它使用多个选择选项列表并隐藏选择选项。
验证失败后,我在选择选项中保留值时遇到问题。
我的选择选项会被触发,就像这个fiddle,
一样第一个选择选项值将触发第二个选择选项,然后第二个选择选项值将触发一个图像。第一个选择选项值保留,但第二个选择选项值被重置。
我可以保留值,但是当用户想要更改为其他选项时,旧选项的值仍保留在那里,它将附加新值。我想要的是如果用户更改其他选项,它将自动重置为null,但在验证失败后,它会保留。
我在这个jquery中使用了明确的值:
$(document).ready(function(){
$('#Service').change(function(){
$('#selectOp1').prop('selectedIndex',0);
$('#selectOp2').prop('selectedIndex',0);
$('#selectOp3').prop('selectedIndex',0);
$('div.second-level-container').children().hide(); //this one for hide the image
)};
但是当验证失败时,所选值也消失了,这非常令人恼火。 有人可以帮助我,我有什么选择,我是jquery的新手
答案 0 :(得分:0)
//You can try this this will reset your select box values also
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#Rank').bind('change', function() {
var elements = $('div.container').children().hide(); // hide all the elements
var value = $(this).val();
if (value.length) { // if somethings' selected
elements.filter('.' + value).show(); // show the ones we want
$('div.container div').children().each(function(){
$.each(this.attributes, function(i, attrib){
if(attrib.name != value)
{
$('#'+value).prop('selectedIndex',0);
}
});
});
var elements = $('div.second-level-container').children().hide(); // hide all the elements
//master reset
}else{
$('#airman').prop('selectedIndex',0);
$('#senior-airman').prop('selectedIndex',0);
var elements = $('div.second-level-container').children().hide();
}
}).trigger('change');
$('.second-level-select').bind('change', function() {
var elements = $('div.second-level-container').children().hide(); // hide all the elements
var value = $(this).val();
if (value.length) { // if somethings' selected
elements.filter('.' + value).show(); // show the ones we want
//reset the child and
}else{
var elements = $('div.second-level-container').children().hide(); // hide all the elements
}
}).trigger('change');
});
</script>
<!-- Site JavaScript -->
<select size="1" id="Rank" title="" name="Rank">
<option value="">-Select Your Rank-</option>
<option value="airman">Airman</option>
<option value="senior-airman">Senior Airman</option>
</select>
<div class="container">
<div class="airman">
<select class="second-level-select" id='airman'>
<option value="">-Select Your Rank-</option>
<option value="basic-ore-1">Basic Ore Miner - Level 1</option>
<option value="basic-ore-2">Basic Ore Miner - Level 2</option>
</select>
</div>
<div class="senior-airman">
<select class="second-level-select" id='senior-airman'>
<option value="">-Select Your Rank-</option>
<option value="omber-miner-1">Omber Miner - Level 1</option>
<option value="omber-miner-2">Omber Miner - Level 2</option>
</select>
</div>
</div>
<div class="second-level-container">
<div class="basic-ore-1">
Line of text for basic ore miner 1
</div>
<div class="basic-ore-2">
Line of text for basic ore miner 2
</div>
<div class="omber-miner-1">
Line of text for omber miner 1
</div>
<div class="omber-miner-2">
Line of text for omber miner 2
</div>
</div>