gpt4 book ai didi

javascript - 设置下拉菜单的选定值

转载 作者:行者123 更新时间:2023-12-02 15:03:41 25 4
gpt4 key购买 nike

下面的代码只能运行 1 次,而不是每次

 var selectedVal = $('#drpGender_0').find("option:selected").text();

if (selectedVal == "Male") {
$('#drpGender_1').find("option:contains('Male')").text('Male').attr("selected", false);
$('#drpGender_1').find("option:contains('Female')").text('Female').attr("selected", "selected");
}
else if (selectedVal == "Female") {

$('#drpGender_1').find("option:contains('Female')").text('Female').attr("selected", false);
$('#drpGender_1').find("option:contains('Male')").text('Male').attr("selected", "selected");

}
};

提前致谢

最佳答案

$('#drpGender_0').on('change', function() {
var selectedVal = $('#drpGender_0').val();

if (selectedVal == "Male") {
$('#drpGender_1').val('Female');
} else if (selectedVal == "Female") {
$('#drpGender_1').val('Male');
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select id="drpGender_0">
<option value="">Select Gender</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>

<select id="drpGender_1">
<option value="">Select Gender</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>

您还可以使用代码

$('#drpGender_0').on('change', function() {
var selectedVal = $('#drpGender_0').val();

if (selectedVal == "Male") {
$('#drpGender_1').find("option:contains('Female')").attr("selected", "selected");

} else if (selectedVal == "Female") {
$('#drpGender_1').find("option:contains('Male')").attr("selected", "selected");
}
});

关于javascript - 设置下拉菜单的选定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35262910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com