打开具有多选选项的模态窗口。

huangapple go评论80阅读模式
英文:

Opening modalwith multiple select Options

问题

你好,以下是翻译好的部分:

<tbody>
          
          <tr>
            <td class="client">client001</td>
            <td class="User">user001</td>
            <td class="Vulnerabilites">
              <select class="form-select" size="4" id="vulselect"  >
                
                <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" id="vulselect"  >
                
                <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>
<script>
$(function () {
    $(".form-select").each(function () {
        $(this).change(function () {
          var e = document.getElementById("vulselect");
          var value = e.value;
          alert(value);
          $('#vulmodal').modal("show");
          
          var elements = document.getElementById("vulselect").options;
          for(var i = 0; i < elements.length; i++){
            elements[i].selected = false;
          }
        });
    });
});

 </script>    

希望这有助于你理解代码的功能。如果需要进一步的帮助,请随时提问。

英文:

Hi I'm trying to open a modal with different Data (loaded from Django) from an Option.

<tbody>
          
          <tr>
            <td class="client">client001</td>
            <td class="User">user001</td>
            <td class="Vulnerabilites">
              <select class="form-select" size="4" id="vulselect"  >
                
                <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" id="vulselect"  >
                
                <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>
<script>
$(function () {
    $(".form-select").each(function () {
        $(this).change(function () {
          var e = document.getElementById("vulselect");
          var value = e.value;
          alert(value);
          $('#vulmodal').modal("show");
          
          var elements = document.getElementById("vulselect").options;
          for(var i = 0; i < elements.length; i++){
            elements[i].selected = false;
          }
        });
    });
});

 </script>    

For my first select in the first table row (user001) the alert shows the correct Values. But when I try it in my second row the JS don't load the option value in the var value.

Sorry Im very new to frontend so any help would be greatly appriciated.

答案1

得分: 2

你的问题是你有多个具有完全相同ID vulselect 的元素

在页面上,ID必须是唯一的,只能有一个具有完全相同ID的元素。你应该考虑使用类(class)代替。

就你的脚本而言,你选择了.form-select元素,当它的值发生变化时,然后通过ID vulselect 重新选择了相同的元素 - 两次 - 你不需要这样做,因为你已经通过$('.form-select')选择了该元素,你现在可以在该函数内部引用this

你可以真正简化这段代码。看下面的示例,我进行了两个更改,但因为我没有提供这个演示的模态框,所以我必须将其注释掉。你不需要在.each()内部再进行.change()。你只需要在顶层调用.change(),该代码将适用于与你给定选择器匹配的每个元素。

$(function () {
  $(".form-select").change(function () {
    console.log(this.value);
    //$('vulmodal').modal('show');

    for (var i = 0; i < this.options.length; i++) {
      this.options[i].selected = false;
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<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>

仅第一个示例中的原因有效是因为你使用了document.getElementById,它只会返回它找到的第一个匹配项。它之所以这样做,是因为在页面上应该只有一个具有该ID的元素。

英文:

Your problem is that you have multiple elements with the exact same ID vulselect

An ID has to be unique on a page, there can only be one with that exact ID. You should probably use a class instead.

As far as your script goes, You select the .form-select element, and when it changes value to then re-selected the same element by ID vulselect - twice - you don't need to do this since you already have the element selected with $(&#39;.form-select&#39;)m you can just reference this inside of that function now!

You can really simplify this code too. See the below demo with both changes, but I had to comment out the modal since I don't have that here for this demo. You don't need to do an .each() and then a .change() inside of that. You can just call .change() at the top level and that code will apply to every element that matches the selector you gave it.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(function () {
  $(&quot;.form-select&quot;).change(function () {
    console.log(this.value);
    //$(&#39;#vulmodal&#39;).modal(&quot;show&quot;);

    for(var i = 0; i &lt; this.options.length; i++){
      this.options[i].selected = false;
    }
  });
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;table&gt;
&lt;tbody&gt;
  &lt;tr&gt;
    &lt;td class=&quot;client&quot;&gt;client001&lt;/td&gt;
    &lt;td class=&quot;User&quot;&gt;user001&lt;/td&gt;
    &lt;td class=&quot;Vulnerabilites&quot;&gt;
      &lt;select class=&quot;form-select&quot; size=&quot;4&quot;&gt;
        &lt;option value=&quot;2705&quot; &gt;Security Vulnerability CVE-2018-1285 for log4net&lt;/option&gt;
        &lt;option value=&quot;73562&quot; &gt;Security Vulnerability CVE-2022-39427 in VirtualBox&lt;/option&gt;
      &lt;/select&gt;
    &lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
    &lt;td class=&quot;client&quot;&gt;client002&lt;/td&gt;
    &lt;td class=&quot;User&quot;&gt;user002&lt;/td&gt;
    &lt;td class=&quot;Vulnerabilites&quot;&gt;
      &lt;select class=&quot;form-select&quot; size=&quot;4&quot;&gt;
        &lt;option value=&quot;68069&quot; &gt;Security Vulnerability CVE-2021-34803 for TeamViewer&lt;/option&gt;
        &lt;option value=&quot;74465&quot; &gt;Security Vulnerability CVE-2023-21899 in VirtualBox&lt;/option&gt;
      &lt;/select&gt;
    &lt;/td&gt;
  &lt;/tr&gt;
  
&lt;/tbody&gt;
&lt;/table&gt;

<!-- end snippet -->


The reason only the first one worked in your example is because you used document.getElementById which will only return the first match it finds. It does this because there should only be one element with that ID on a page anyway.

huangapple
  • 本文由 发表于 2023年5月18日 06:26:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76276563.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定