使用 jQuery 处理多选下拉菜单的模态框弹窗:解决 ID 冲突和简化代码

使用 jQuery 处理多选下拉菜单的模态框弹窗:解决 ID 冲突和简化代码

本文旨在解决在使用 jquery 处理包含多个下拉选择框的 html 表格时,由于 ID 冲突导致事件处理不正确的问题。我们将详细讲解如何通过使用 class 代替 ID,并优化 jQuery 代码,从而实现正确获取每个下拉选择框的值,并触发相应的模态框弹窗,同时避免因重复选择元素而导致的代码冗余。

避免 ID 冲突:使用 Class 代替 ID

在 HTML 中,id 属性必须是唯一的。当你在同一个页面中使用多个相同 id 的元素时,JavaScript 可能会选择第一个匹配的元素,从而导致事件处理不正确。例如,在提供的代码中,多个 <select> 元素都使用了 id=”vulselect”,这会导致 JavaScript 只能获取到第一个下拉选择框的值。

解决这个问题的方法是将 id 属性替换为 class 属性。class 属性可以用于将多个元素归为同一类,并使用 css 或 JavaScript 同时操作这些元素。

修改 HTML 代码:

将 <select id=”vulselect” 修改为 <select class=”form-select”。

<tbody>   <tr>     <td class="client">client001</td>     <td class="User">user001</td>     <td class="Vulnerabilites">       <select class="form-select" size="4">         <option value="2705" >Security Vulnerability CVE-2018-1285 for log4net</option>         <option value="73562" >Security Vulnerability CVE-2022-39427 in VirtualBox</option>       </select>     </td>   </tr>    <tr>     <td class="client">client002</td>     <td class="User">user002</td>     <td class="Vulnerabilites">       <select class="form-select" size="4">         <option value="68069" >Security Vulnerability CVE-2021-34803 for TeamViewer</option>         <option value="74465" >Security Vulnerability CVE-2023-21899 in VirtualBox</option>       </select>     </td>   </tr>  </tbody>

优化 jQuery 代码:简化事件处理

原始的 jQuery 代码使用了 .each() 循环来遍历每个 .form-select 元素,并在循环内部绑定 change 事件。这是一种冗余的做法,因为可以直接在 .form-select 选择器上绑定 change 事件,从而简化代码。

此外,原始代码中在 change 事件处理函数中,使用 document.getElementById(“vulselect”) 再次获取了 <select> 元素。由于我们已经通过 $(“.form-select”) 选择器获取了元素,可以直接使用 this 关键字来引用当前触发事件的元素。

优化后的 jQuery 代码:

$(function () {   $(".form-select").change(function () {     var selectedValue = this.value;     console.log("Selected value:", selectedValue);     // $('#vulmodal').modal("show"); // 取消注释以显示模态框      // 重置所有选项的选择状态     for(var i = 0; i < this.options.Length; i++){       this.options[i].selected = false;     }   }); });

代码解释:

  • $(“.form-select”).change(function () { … });: 这行代码选择所有 class 为 form-select 的元素,并为它们的 change 事件绑定一个处理函数。
  • this.value: 在 change 事件处理函数中,this 关键字指向当前触发事件的 <select> 元素。this.value 获取当前选中的选项的值。
  • console.log(“Selected value:”, selectedValue);: 将选中的值输出到控制台,用于调试。
  • // $(‘#vulmodal’).modal(“show”);: 这行代码被注释掉了,因为没有提供模态框的具体实现。如果你的页面中存在 id 为 vulmodal 的模态框,取消注释即可显示模态框。
  • for(var i = 0; i < this.options.length; i++){ this.options[i].selected = false; }:这段代码循环遍历当前select的所有选项,并将每个选项的 selected 属性设置为 false,从而取消所有选项的选择。

完整示例代码

<!DOCTYPE html> <html> <head>   <title>Multiple Select Options Modal</title>   <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> </head> <body>  <table>   <tbody>     <tr>       <td class="client">client001</td>       <td class="User">user001</td>       <td class="Vulnerabilites">         <select class="form-select" size="4">           <option value="2705" >Security Vulnerability CVE-2018-1285 for log4net</option>           <option value="73562" >Security Vulnerability CVE-2022-39427 in VirtualBox</option>         </select>       </td>     </tr>      <tr>       <td class="client">client002</td>       <td class="User">user002</td>       <td class="Vulnerabilites">         <select class="form-select" size="4">           <option value="68069" >Security Vulnerability CVE-2021-34803 for TeamViewer</option>           <option value="74465" >Security Vulnerability CVE-2023-21899 in VirtualBox</option>         </select>       </td>     </tr>    </tbody> </table>  <script> $(function () {   $(".form-select").change(function () {     var selectedValue = this.value;     console.log("Selected value:", selectedValue);     // $('#vulmodal').modal("show"); // 取消注释以显示模态框      // 重置所有选项的选择状态     for(var i = 0; i < this.options.length; i++){       this.options[i].selected = false;     }   }); }); </script>  </body> </html>

注意事项

  • 确保在引入 jQuery 库之后再编写 jQuery 代码。
  • 如果需要显示模态框,请确保你的页面中存在 id 为 vulmodal 的模态框,并取消代码中的注释 // $(‘#vulmodal’).modal(“show”);。
  • 根据实际需求,可以修改 change 事件处理函数中的代码,例如将选中的值传递给模态框进行显示。

总结

通过将 id 属性替换为 class 属性,并优化 jQuery 代码,可以解决在使用 jQuery 处理包含多个下拉选择框的 HTML 表格时遇到的 ID 冲突问题,并简化代码。 优化后的代码更加简洁、易懂,并且能够正确地获取每个下拉选择框的值,并触发相应的模态框弹窗。 这种方法不仅适用于下拉选择框,也适用于其他需要处理多个相同类型元素的场景。

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享