点击不同的中的时,获取的值 huangapple 117266文章 0评论 2023年5月24日 19:46:54go评论69阅读模式 英文: 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 <td> that is housing an <select> with <options>. 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 <option>. My JavaScript can show the value of the <option> but I dont know how to access the hostname. <!-- begin snippet: js hide: false console: true babel: false --> <!-- language: lang-js --> $(function (e) { $(".form-select").change(function (event) { alert(this.value); //shows the Value of the selected Option for(var i = 0; i < 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> <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"><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">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"><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">Security Vulnerability CVE-2021-34803 for TeamViewer</option> <option value="74465">Security Vulnerability CVE-2023-21899 in VirtualBox</option> </select> </td> </tr> </tbody> </table> <!-- 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) { $(".form-select").change(function (event) { alert(this.value); //shows the Value of the selected Option alert($(this).closest('tr').find('td.Rechnernummer a').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) { $(".form-select").change(function (event) { alert(this.value); // shows the value of the selected option var selectedOptionText = $('option:selected', 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 <a href="javascript:void(0)" ...>. 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 <a href="javascript:void(0)" ...>. 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 --> <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> <!-- end snippet --> 通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。 点赞 https://go.coder-hub.com/76323183.html 复制链接 复制链接 本文由 huangapple 发表于 2023年5月24日 19:46:54 转载请务必保留本文链接:https://go.coder-hub.com/76323183.html javascript jquery 当单击按钮时切换元素的可见性。 go 53 05/29 使用 Tauri 中的 fs 模块将画布数据保存为 png 或 jpg。 go 135 07/13 JavaScript: detect page is scrolling on scroll-behavior: smooth and deprecate scroll event in this case go 64 03/31 Regex expression to replace special characters except first and last character found go 64 05/11 What's the correct way to type hint an empty list as a literal in python? 如何在Highcharts Gantt中更改本地化的星期名称 如何在同一个流中使用多个过滤器和映射函数? 如何使用Map/Set来将代码优化到O(n)? .NET MAUI Android在GitHub Actions上构建失败,错误代码为1。 如何在Playwright视觉比较中屏蔽多个定位器? 在C++中,可以使用可变模板参数来检索类型的内部类型。 selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: stale element not found Creating and opening a URL to log in to Website via Basic Auth with Robot Framework/Selenium (Python) AG Grid 在上下文菜单中以大文本形式打开 发表评论 匿名网友 确定 昵称 邮箱 网址 Address 取消
JavaScript: detect page is scrolling on scroll-behavior: smooth and deprecate scroll event in this case go 64 03/31