点击不同的中的

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

Get Value of <td> when clicking an <option> in different <td>

问题

我有一个包含<td>的表格,其中包含一个带有<select><options>的元素。

我想要创建一个请求,将点击的选项的值和所点击的<option>所在行的主机名发送到我的Django后端。

我的JavaScript可以显示<option>的值,但我不知道如何访问主机名。

$(function (e) {
  $(".form-select").change(function (event) {
    alert(this.value); // 显示所选选项的值
    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 class="table">
  <thead>
    <tr>
      <th scope="col">主机名</th>
      <th scope="col">用户</th>
      <th scope="col">漏洞</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td class="Rechnernummer"><a href="javascript:void(0)" class="link-offset-2 link-offset-3-hover link-underline link-underline-opacity-0 link-underline-opacity-75-hover" onclick="clientInfo(event);">Host001</a></td>
      <td class="User">User001</td>
      <td class="Vulnerabilites">
        <select class="form-select" size="4">
          <option value="2705">log4net的安全漏洞CVE-2018-1285</option>
          <option value="73562">VirtualBox中的安全漏洞CVE-2022-39427</option>
        </select>
      </td>
    </tr>
    <tr>
      <td class="Rechnernummer"><a href="javascript:void(0)" class="link-offset-2 link-offset-3-hover link-underline link-underline-opacity-0 link-underline-opacity-75-hover" onclick="clientInfo(event);">Host002</a></td>
      <td class="User">User002</td>
      <td class="Vulnerabilites">
        <select class="form-select" size="4">
          <option value="68069">TeamViewer的安全漏洞CVE-2021-34803</option>
          <option value="74465">VirtualBox中的安全漏洞CVE-2023-21899</option>
        </select>
      </td>
    </tr>
  </tbody>
</table>
英文:

I've got an Table with a &lt;td&gt; that is housing an &lt;select&gt; with &lt;options&gt;.

I want to create a request to my Django backend with the value of the Option clicked and the hostname in the row of the clicked &lt;option&gt;.

My JavaScript can show the value of the &lt;option&gt; but I dont know how to access the hostname.

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

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

$(function (e) {
  $(&quot;.form-select&quot;).change(function (event) {
    alert(this.value); //shows the Value of the selected Option
    for(var i = 0; i &lt; this.options.length; i++){ //deselect the option
      this.options[i].selected = false;
    }
  });
});

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

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

&lt;table class=&quot;table&quot;&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th scope=&quot;col&quot;&gt;Hostname&lt;/th&gt;
      &lt;th scope=&quot;col&quot;&gt;User&lt;/th&gt;
      &lt;th scope=&quot;col&quot;&gt;Vulnerabilites&lt;/th&gt;

    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;

    &lt;tr&gt;
      &lt;td class=&quot;Rechnernummer&quot;&gt;&lt;a href=&quot;javascript:void(0)&quot; class=&quot;link-offset-2 link-offset-3-hover link-underline link-underline-opacity-0 link-underline-opacity-75-hover&quot; onclick=&quot;clientInfo(event);&quot;&gt;Host001&lt;/a&gt;&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;Rechnernummer&quot;&gt;&lt;a href=&quot;javascript:void(0)&quot; class=&quot;link-offset-2 link-offset-3-hover link-underline link-underline-opacity-0 link-underline-opacity-75-hover&quot; onclick=&quot;clientInfo(event);&quot;&gt;Host002&lt;/a&gt;&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 -->

答案1

得分: 2

你可以从事件处理程序中的 "当前" 元素开始,遍历到一个共同的父元素,然后再回到你要查找的 "相邻" 元素。例如:

$(function (e) {
  $(".form-select").change(function (event) {
    alert(this.value); // 显示所选选项的值
    alert($(this).closest('tr').find('td.Rechnernummer a').text()); // 显示主机名

    // 等等。
  });
});

使用 .closest() 方法选择第一个匹配的父元素(向上遍历 DOM),然后使用 .find() 方法在该元素内部选择匹配的元素(向下遍历 DOM)。

英文:

You can "traverse the DOM" from the "current" element in the event handler, up to a common parent, and back down to the "neighboring" element that you're looking for. For example:

$(function (e) {
  $(&quot;.form-select&quot;).change(function (event) {
    alert(this.value); //shows the Value of the selected Option
    alert($(this).closest(&#39;tr&#39;).find(&#39;td.Rechnernummer a&#39;).text()); // shows the Hostname

    // etc.
  });
});

The use of .closest() selects the first matching parent element (traversing "up" the DOM) and then .find() selects a matching element within that element (traversing back "down" the DOM).

答案2

得分: 0

你可以使用.text()函数访问所选选项的文本,如下所示:

$(function (e) {
  $(".form-select").change(function (event) {
    alert(this.value); // 显示所选选项的值
    var selectedOptionText = $('option:selected', this).text(); // 获取所选选项的文本。
    console.log(selectedOptionText);
  });
});
英文:

You can access the selected option text with .text() function as

$(function (e) {
  $(&quot;.form-select&quot;).change(function (event) {
    alert(this.value); // shows the value of the selected option
    var selectedOptionText = $(&#39;option:selected&#39;, this).text(); //gets selected option text.
    console.log(selectedOptionText);
  });
});

答案3

得分: 0

以下是您要翻译的内容:

For the record: it is generally not a good idea to use inline event handlers. Furthermore, you don't really need &lt;a href=&quot;javascript:void(0)&quot; ...&gt;.

Here is an example not using JQuery and using event delegation for the (change) handling. The empty href was removed. Your table lacked a value for the second host, added it in the example.

以下是示例代码:

document.addEventListener(`change`, handle);

function handle(evt) {
  const currentSelector = evt.target.closest(`.form-select`);

  if (currentSelector) {
    const row = currentSelector.closest(`tr`);
    const rechnerNummer = row.querySelector(`.Rechnernummer`).textContent.trim();
    const selectedValue = currentSelector.value;
    console.clear();
    // here you can continue sending the values, 
    // for demo they are logged
    return console.log(`selected: ${
      selectedValue ?? `nothing`}; rechner#${
      rechnerNummer ?? `Empty`}`);
  }
  return true;
}
<table class="table">
  <thead>
    <tr>
      <th scope="col">Hostname</th>
      <th scope="col">User</th>
      <th scope="col">Vulnerabilites</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td class="Rechnernummer">Host001</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="Rechnernummer">Host002</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>

希望这对您有所帮助。

英文:

For the record: it is generally not a good idea to use inline event handlers. Furthermore, you don't really need &lt;a href=&quot;javascript:void(0)&quot; ...&gt;.

Here is an example not using JQuery and using event delegation for the (change) handling. The empty href was removed. Your table lacked a value for the second host, added it in the example.

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

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

document.addEventListener(`change`, handle);

function handle(evt) {
  const currentSelector = evt.target.closest(`.form-select`);

  if (currentSelector) {
    const row = currentSelector.closest(`tr`);
    const rechnerNummer = row.querySelector(`.Rechnernummer`).textContent.trim();
    const selectedValue = currentSelector.value;
    console.clear();
    // here you can continue sending the values, 
    // for demo they are logged
    return console.log(`selected: ${
      selectedValue ?? `nothing`}; rechner#${
      rechnerNummer ?? `Empty`}`);
  }
  return true;
}

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

&lt;table class=&quot;table&quot;&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th scope=&quot;col&quot;&gt;Hostname&lt;/th&gt;
      &lt;th scope=&quot;col&quot;&gt;User&lt;/th&gt;
      &lt;th scope=&quot;col&quot;&gt;Vulnerabilites&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td class=&quot;Rechnernummer&quot;&gt;Host001&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;Rechnernummer&quot;&gt;Host002&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 -->

huangapple
  • 本文由 发表于 2023年5月24日 19:46:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76323183.html
匿名

发表评论

匿名网友

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

确定