我正在使用以下 jQuery 代码从选择中删除选项并且运行良好。但是,与其仅在更改 theOption2 select 时执行,我还希望它在加载页面时根据所选的所选项目工作。我尝试使用脚本的副本并将 .change 更改为 .load 并尝试使用 (window).load 而没有得到预期的结果。本质上,我需要脚本在更改 Options1 和加载页面时执行。如有任何帮助,我们将不胜感激。
<script type="text/javascript">
$(document).ready(function() {
//copy the second select, so we can easily reset it
var selectClone = $('#theOptions2').clone();
$('#theOptions1').change(function() {
var val = parseInt($(this).val());
//reset the second select on each change
$('#theOptions2').html(selectClone.html())
switch(val) {
//if 2 is selected remove C
case 2 : $('#theOptions2').find('option:contains(c)').remove();break;
//if 3 is selected remove A
case 3 : $('#theOptions2').find('option:contains(a)').remove();break;
}
});
});
</script>
<select id="theOptions1">
<option value="1">1</option>
<option value="2" selected="selected">2</option>
<option value="3">3</option>
</select>
<br />
<select id="theOptions2">
<option>a</option>
<option>b</option>
<option>c</option>
</select>
最佳答案
在$(document).ready(...
:
$('#theOptions1').trigger('change');
关于javascript - jQuery 根据更改和加载时选择的另一个 SELECT 删除 SELECT 选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3010734/