的ID我有一個結構:獲取以前選擇標籤
<div class="input_fields_wrap">
<div class="row">
<select id="house-1-room-1"><?php echo $options; ?></select>
<input type='text' class='name' name='house-1-room-size-1' placeholder='Room Size'>
</div>
</div>
<button class="add_field_button">Add Another Room</button>
當add_field_button
點擊,我試圖讓以前select
標籤的ID,在我的情況下,它是house-1-room-1
使用jQuery(或vanila JS) 。
我已經試過:
$(this).parent().prev("select").attr('id');
和
$(this).prev("div").closest("select").attr('id');
及以上無濟於事許多其他組合。
請幫忙!
我認爲你需要像這樣: '$(本).prev()。find(「select」)。attr('id');' –
請注意,要找到您首先找到節點的id。因此,提取id,然後用'$('#myid')搜索文檔,找到與提取id相同的確切節點,這是浪費時間的很大一部分。 – James