jquery - jquery隐藏选择以前的值和onchange显示新值下拉
问题描述
我在 jquery 上遇到了一个问题。我想隐藏选择值(以前的值),但是如果更改下拉列表,它会显示新值并从所有下拉列表中隐藏以前的值。
HTML 下拉菜单
<select class="gr-fields-select">
<option value="">Select</option>
<option value="18355">Product Management</option>
<option value="18356">QA</option>
<option value="18357">Sales</option>
<option value="98595">Support</option>
<option value="05223">Test group</option>
<option value="05224">Test group one user</option>
</select>
</div>
这是我的 jquery 函数
function hideSelectedValues(){
$("select").each(function(){
let selectValue = $(this).val();
if(selectValue == "Select" || selectValue == "") return;
$(this).addClass("currentSelection");
$("select").each(function(){
if(!$(this).hasClass("currentSelection")){
let previousValue = $(this).val();
$(this).find(`option[value='${previousValue}']`).hide();
}
else{
$(this).on('change',function () { //change and show newValue
let newValue = $(this).val();
$(this).find(`option[value='${newValue}']`).show();
$(this).find(`option[value='${previousValue}']`).hide(); //Error undefined
});
}
});
$(this).removeClass("currentSelection");
})
}
此功能不起作用。我无法从 IF 条件中获取先前的值并在 ELSE 正文中传递先前的值以隐藏先前的值,但它给出了错误先前的值未定义
我在这个功能中犯了什么错误?
谁能帮我?
解决方案
onchange="if($(this).val()=='1') $('#18355').show();else $('#18355').hide();
推荐阅读
- python-3.x - 使用 2 个堆栈创建一个队列
- c++ - 程序忽略 cin.get() 之后的所有内容并跳回菜单
- flutter - 带有用户进度指示器的 Flutter 自定义 AppBar 显示
- javascript - 使用graphQL和sequelize查找排序查询中相对于父查询的先前条目的值
- python - 用curve_fit拟合函数,但拟合曲线错误
- android - 为什么使用 AndroidX 时嵌套的 PreferenceScreen 打不开?
- python - rpy2 代码片段返回一个空对象
- javascript - 如何使用 puppeteer 检查音频流是否正在播放?
- python - 将 2 个 numpy 数组相乘并返回每个维度的总和
- scala - 你如何使用 sbt-pgp 和不同的命令来调用 gpg?我试过 gpgCommand 没有成功