我有一个下拉列表,我需要捕获页面加载时和页面更改时的值。如果未选择报告字段,则不显示州列表或年份列表,如果选择了报告字段(无论是加载时还是用户从下拉列表中选择它时),则显示相应的 div。
感谢任何帮助,谢谢。
<script type="text/javascript">
$(document).ready(function() {
checkReport();
});
</script>
<select name="report" id="report">
<option></option>
<option value="state">State</option>
<option value="year">Year</option>
</select>
<div id="state-list">NY, CA, TX</div>
<div id="year-list">2012, 2013, 2014</div>
function checkReport() {
$('#report').live('change', function() {
$('#year-list, #state-list).hide();
var report = $('#report').val();
if (report) {
$('#' + type_of_report + '-list').show();
} else {
$('#year-list, #state-list).hide();
}
});
}
最佳答案
$(document).ready(function() {
checkReport();
$('#report').live('change', checkReport);
});
function checkReport() {
var report = $('#report').val();
$('#year-list, #state-list').hide();
if (report) {
$('#' + report + '-list').show();
}
}
我猜这就是你所需要的
关于javascript - 在加载和单击时检查下拉列表的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10249664/