点击JavaScript数组时如何获取数据

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

How to get data when I click on array in javascrpit

问题

Sure, here's the translated code portion:

$('.table .detailsButton').on('click', function(event) {

    let productid = document.querySelectorAll('.reqproid').value;

    alert(productid);

});

And the translated text:

"This is my Data on spring

View

Example that When I Click on View, after that it shows an alert with reqproid."

英文:
$('.table .detailsButton').on('click',function(event){
		
		let productid = document.querySelectorAll('.reqproid').value;
		
		alert(productid);
		
	});

This is my Data on spring

<tr th:each="pendingStock,iStat:${listPenddingByID}" name="line_items">
    <td th:text="${pendingStock.reqproid}" hidden name="reqproid" id="reqproid"class="reqproid"></td>
    <td>
		<a style="padding: 10px;" id="detailsButton" class="detailsButton"><i class="ti- pencilalt">View</i></a>
    </td>
</tr>

Example that When I Click oh view after that is show alert reqproid

答案1

得分: 0

由于您想将您的“查看”按钮与与按钮在同一行的特定值关联起来,您不应该使用`document.querySelectorAll('.reqproid')`,因为它会返回页面上该类的所有元素。

以下是您可以做的替代方法:

<!-- 开始代码段:js 隐藏:false 控制台:true Babel:false -->

<!-- 语言:lang-js -->

    $('.table .detailsButton').on('click', function(event) {
      const row = event.target.parentNode.parentNode.parentNode;
      const productid = row.querySelector('.reqproid').innerText;
      alert(productid);
    });

<!-- 语言:lang-html -->

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

    <body>
      <table class="table">
        <tr th:each="pendingStock,iStat:${listPenddingByID}" name="line_items">
          <td th:text="${pendingStock.reqproid}" hidden name="reqproid" id="reqproid" class="reqproid">1234</td>
          <td>
            <a style="padding: 10px;" id="detailsButton" class="detailsButton"><i class="ti- pencilalt">View</i></a>
          </td>
        </tr>
      </table>
    </body>

    </html>

<!-- 结束代码段 -->

请告诉我这是否对您有所帮助。
英文:

Since you want to associate your View button with a specific value that is on the same row with the button, you shouldn't use document.querySelectorAll('.reqproid') that will return you all elements of this class on the page.

Here is what you can do instead:

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

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

$(&#39;.table .detailsButton&#39;).on(&#39;click&#39;, function(event) {
  const row = event.target.parentNode.parentNode.parentNode;
  const productid = row.querySelector(&#39;.reqproid&#39;).innerText;
  alert(productid);
});

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

&lt;html&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;

&lt;body&gt;
  &lt;table class=&quot;table&quot;&gt;
    &lt;tr th:each=&quot;pendingStock,iStat:${listPenddingByID}&quot; name=&quot;line_items&quot;&gt;
      &lt;td th:text=&quot;${pendingStock.reqproid}&quot; hidden name=&quot;reqproid&quot; id=&quot;reqproid&quot; class=&quot;reqproid&quot;&gt;1234&lt;/td&gt;
      &lt;td&gt;
        &lt;a style=&quot;padding: 10px;&quot; id=&quot;detailsButton&quot; class=&quot;detailsButton&quot;&gt;&lt;i class=&quot;ti- pencilalt&quot;&gt;View&lt;/i&gt;&lt;/a&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/table&gt;
&lt;/body&gt;

&lt;/html&gt;

<!-- end snippet -->

Please let me know if this helps.

huangapple
  • 本文由 发表于 2023年5月15日 12:16:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76250851.html
匿名

发表评论

匿名网友

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

确定