該頁面的HTML如下:多種形式,獲取輸入值
<div class="list-block media-list">
<ul>
<form method="post" action="">
<li >
<div class="item-content">
<div class="item-media">
<img width="80">
</div>
<div class="item-inner">
<div class="item-title-row">
<div class="item-title">Element title</div>
<div class="item-after">Unexcused</div>
</div>
<div class="item-subtitle">Subtitle</div>
<div class="item-text">Additional description text</div>
</div>
</div>
</li>
<input type="hidden" name="status" id="status" value="" />
<input type="hidden" name="client_id" id="client_id" value="23" />
</form>
<form method="post" action="">
<li class="red">
<div class="item-content">
<div class="item-media">
<img width="80">
</div>
<div class="item-inner">
<div class="item-title-row">
<div class="item-title">Element title</div>
<div class="item-after">Excused</div>
</div>
<div class="item-subtitle">Subtitle</div>
<div class="item-text">Additional description text</div>
</div>
</div>
</li>
<input type="hidden" name="status" id="status" value="" />
<input type="hidden" name="client_id" id="client_id" value="27" />
</form>
</ul>
</div>
這裏是JS:
$(document).ready(function() {
$('.media-list li').on('click', function() {
var buttonValue = $(this).closest('#client_id').prop("value");
alert(buttonValue);
});
});
當我點擊<li>
它應該以提醒client_id
的價值,但它始終是23
,如果我點擊第一個<li>
,這是正確的數字。有沒有其他方法可以解決這個問題?
HTML ID應該是唯一的 – 2014-09-03 13:06:17
**和**'.closest ()''做了你想做的事情:('。('。child')。closest('。parent')'返回匹配選擇器'.parent'的'.child'的第一個祖先,如果有的話 – PeterKA 2014-09-03 13:09:04