对于这个问题,我使用的是official example的稍微修改的版本。
如果从选择菜单“选择速度”中选择的选项不是Fast
,我会尝试停用选择菜单“选择标题”。
如果选择了Fast
,则应该再次激活选择。
尝试:
$( function() {
$( "#speed" ).selectmenu();
$( "#salutation" ).selectmenu();
$speed = $('#speed');
$salutation = $('#salutation');
test();
$speed.on('selectmenuchange', function() {
test();
});
function test()
{
let selectedOption = $speed.find('option:selected').text().trim().toLowerCase();
if ( ! selectedOption.includes('fast')) {
$salutation.prop('disabled', 'disabled');
} else {
$salutation.removeAttr('disabled');
$salutation.next().find('.ui-selectmenu-text').trigger("click");
}
}
});
fieldset {
border: 0;
}
label {
display: block;
margin: 30px 0 0 0;
}
.overflow {
height: 200px;
}
#
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<div class="demo">
<form action="#">
<fieldset>
<label for="speed">Select a speed</label>
<select name="speed" id="speed">
<option>Slower</option>
<option>Slow</option>
<option selected="selected">Medium</option>
<option>Fast</option>
<option>Faster</option>
</select>
<label for="salutation">Select a title</label>
<select name="salutation" id="salutation">
<option disabled selected>Please pick one</option>
<option>Mr.</option>
<option>Mrs.</option>
<option>Dr.</option>
<option>Prof.</option>
<option>Other</option>
</select>
</fieldset>
</form>
</div>
但是如您所见,它无法按预期运行。任何提示都欢迎。
答案 0 :(得分:0)
我找到了解决方案。我必须将函数test()
更改为此:
function test()
{
let selectedOption = $speed.find('option:selected').text().trim().toLowerCase();
//Hide
if ( ! selectedOption.includes('fast')) {
$salutation.selectmenu('disable');
} else {
$salutation.selectmenu('enable');
}
}
$( function() {
$( "#speed" ).selectmenu();
$( "#salutation" ).selectmenu();
$speed = $('#speed');
$salutation = $('#salutation');
test();
$speed.on('selectmenuchange', function() {
test();
});
function test()
{
let selectedOption = $speed.find('option:selected').text().trim().toLowerCase();
//Hide
if ( ! selectedOption.includes('fast')) {
$salutation.selectmenu('disable');
} else {
$salutation.selectmenu('enable');
}
}
});
fieldset {
border: 0;
}
label {
display: block;
margin: 30px 0 0 0;
}
.overflow {
height: 200px;
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<div class="demo">
<form action="#">
<fieldset>
<label for="speed">Select a speed</label>
<select name="speed" id="speed">
<option>Slower</option>
<option>Slow</option>
<option selected="selected">Medium</option>
<option>Fast</option>
<option>Faster</option>
</select>
<label for="salutation">Select a title</label>
<select name="salutation" id="salutation">
<option disabled selected>Please pick one</option>
<option>Mr.</option>
<option>Mrs.</option>
<option>Dr.</option>
<option>Prof.</option>
<option>Other</option>
</select>
</fieldset>
</form>
</div>