Javascript-форум (https://javascript.ru/forum/)
-   jQuery (https://javascript.ru/forum/jquery/)
-   -   Get TD val in table (https://javascript.ru/forum/jquery/79698-get-td-val-table.html)

Asew 11.03.2020 11:57

Get TD val in table
 
Как взят val td при нажатии на кнопку:

<div id ="test">
<table id ="city">
<tr>
<th>Город</th>
<th>Значение</th>
<th>Действия</th>
</tr>
<tr>
<td>Лондон</td>
<td>1</td>
<td>
<table>
<td><button title = "Удалить" class = "btn_del"></button></td>
<td><button title = "Добавить" class = "btn_add"></button></td>
</table>
</td>
</tr>
<td>Париж</td>
<td>2</td>
<td>
<table>
<td><button title = "Удалить" class = "btn_del"></button></td>
<td><button title = "Добавить" class = "btn_add"></button></td>
</table>
</td>
</tr>
</div>

laimas 11.03.2020 12:06

Цитата:

Сообщение от Asew
<div id ="test">
<tr>

Уже ошибка, не может в элементах таблицы, кроме как в TD, иных не табличных элементов. И зачем кнопки в отдельную таблицу, она лишняя, вполне без нее можно обойтись.

Asew 11.03.2020 12:08

Спасибо, подправил. Сделано в виде доп. таблицы для отступов.

рони 11.03.2020 12:19

Asew,
https://javascript.ru/forum/jquery/7...nyjj-blok.html

Asew 11.03.2020 12:24

пробовал, не срабатывает. Возможно из-за того что кнопка вшита во внутреннюю таблицу.

рони 11.03.2020 12:35

Цитата:

Сообщение от Asew
пробовал, не срабатывает.

свой html исправьте и пропишите свои селекторы.

<!DOCTYPE html>
<html>
<head>
  <title>Untitled</title>
  <meta charset="utf-8">
  <style type="text/css">
  </style>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

  <script>
$(function() {
$('tr').on('click', 'tr .btn_del,tr .btn_add', function(event) {
   var txt = event.delegateTarget.cells[0].textContent;
   alert(txt);
})

});
  </script>
</head>
<body>
<div id ="test">
<table id ="city">
<tr>
<th>Город</th>
<th>Значение</th>
<th>Действия</th>
</tr>
<tr>
<td>Лондон</td>
<td>1</td>
<td>
<table><tr>
<td><button title = "Удалить" class = "btn_del"></button></td>
<td><button title = "Добавить" class = "btn_add"></button></td></tr>
</table>
</td>
</tr>
<tr>
<td>Париж</td>
<td>2</td>
<td>
<table><tr>
<td><button title = "Удалить" class = "btn_del"></button></td>
<td><button title = "Добавить" class = "btn_add"></button></td></tr>
</table>
</td>
</tr>
</table>
</div>

</body>
</html>

Asew 11.03.2020 13:46

сработало, спасибо


Часовой пояс GMT +3, время: 14:24.