這是我的代碼,如下所示。這裏的表格顯示日期時間字符串和關閉按鈕。當我點擊關閉按鈕時,它會刪除關注行。但在刪除之前我需要變量中的日期。當使用jQuery按鈕單擊按鈕時從表中獲取值
$(document).on('click', 'button.removebutton', function() {
\t alpha = $(this).closest('tr #newdate').val();
\t alert(alpha);
$(this).closest('tr').remove();
return false;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="schedule"> \t
\t <tr>
<td>
<input type="text" name="newdate" id="newdate" value="2017-06-26">
</td>
<td>
<input type="text" name="newtime" id="newtime" value="06:00">
</td>
<td>
<button type="button" class="removebutton">X</button>
</td>
</tr>
<tr>
<td>
<input type="text" name="newdate" id="newdate" value="2017-06-28">
</td>
<td>
<input type="text" name="newtime" id="newtime" value="12:00,12:30,13:00,13:30">
</td>
<td>
<button type="button" class="removebutton">X</button>
</td>
</tr>
<tr>
</table>
如果表恰好嵌套在另一個表中,父母()會找到多個''s。所以在這種情況下不能依賴它。最接近()會更合適。 – ADyson
這是真的,但在這種情況下,只有一個表。問候 – Albeis
在這個例子中,是的,但它只是一個片段。您不知道頁面的其他部分是什麼樣子,或者這是否會成爲插件的一部分,這些插件可能會被添加到其他頁面的任何位置。儘可能使代碼儘可能地萬無一失是一種很好的做法。 – ADyson