今天我遇到了关于html select
的问题这是代码:
<select id="optdon" onchange="showimage(this);" name="optdon" style="width:<?php echo $drop_width; ?>px;">
<option value="">--Select don--</option>
<?php foreach($for_don as $donname) { if(isset($donname['don_name']) && $donname['status'] == 1) { ?>
<option value="<?php echo $donname['don_id']; ?>"><?php echo $donname['don_name']?></option>
<?php } } ?>
</select>
<br /><br />
<div align="center">
<?php $i = 0; foreach($for_don as $donimage) { if(isset($donimage['don_image'])) { ?>
<div class="<?php echo $i; ?>" style="display:none;" id="<?php echo $donimage['don_id']; ?>"><img src="<?php echo $donimage['don_image']?>" /></div>
<?php $i = $i + 1;} }?>
function showimage(ids){
clearall();
if(ids.value != ''){
jQuery('#'+ids.value).show();
}
}
项目是什么?我需要选择第一个选项!!!
很容易,但是当我设置第一个选项时,它的图像不会在首页加载的页面中加载。选项的图像出现在更改选项上。
我有两个选择第一个选项的答案(当这只是一个选项时)
建议1
<?php $x = 0; foreach($for_don as $donname){ if(isset($donname['don_name']) && $donname['status'] == 1) { $x = $x + 1; }} ?>
<select id="optdon" onload="showimage(this);" name="optdon" style="width:<?php echo $drop_width; ?>px;">
<option value="">--Select don--</option>
<?php $z = 0; $selected='selected' ?>
<?php foreach($for_don as $donname) { if(isset($donname['don_name']) && $donname['status'] == 1) { $z = $z + 1; ?>
<option <?php if($z==1 && $x == 1)echo $selected; ?> value="<?php echo $donname['don_id']; ?>" ><?php echo $donname['don_name']?></option>
<?php } } ?>
</select>
<br /><br />
<div align="center">
<?php $i = 0; foreach($for_don as $donimage) { if(isset($donimage['don_image'])) { ?>
<div class="<?php echo $i; ?>" style="display:none;" id="<?php echo $donimage['don_id']; ?>"><img src="<?php echo $donimage['don_image']?>" /></div>
<?php $i = $i + 1;} }?>
</div>
建议2:
<?php $x = 0; foreach($for_don as $donname){ if(isset($donname['donation_name']) && $donname['status'] == 1) { $x = $x + 1; }} ?>
<?php if($x == 1) { ?>
??????
<?php } else { ?>
谢谢
答案 0 :(得分:0)
添加此JavaScript:
var optdon = document.getElementById('optdon');
optdon.onchange.call(optdon);
这将触发onChange
事件处理程序,允许初始选项生效。