簡體   English   中英

單擊按鈕 parentElement 執行 2 次

[英]click on button parentElement execute 2 times

在此處輸入圖像描述 在此處輸入圖像描述

我在 javascript 中遇到問題。 如果用戶單擊send按鈕,我想這樣做,那么代碼將執行time ,並且只顯示一次名字值

 $(document).on("click", "#send_button", function (e){ //name shows 2 times console.log(this.parentElement.parentElement.childNodes[3].innerHTML); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script> <td> <button id="send_button" class="btn btn-primary pull-right">Send</button> </td>

JSON 數據:

{name: "test", body: "test"}
{name: "test2", body: "test2"}


代碼 Output:

test //execute name 2 times

我應該怎么辦?

將您的按鈕更改為:

<button class="send-button btn btn-primary pull-right">Send</button>

所以send-button是 class。

然后你的 javascript 應該是:

$(document).on("click", ".send_button", function (e) {   
    console.log($(this).closest("tr").find("td.name").text());
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM