我需要在同一个表行中找到隐藏输入的值,但似乎我无法通过我的尝试访问它。
function setDateTimeOn(elm) {
var formattedDate = GetCurrentDateTime(); //get formatted date
$(elm) //clicked button
.parent("td") // container td
.next() // next td
.find("span")
.text(formattedDate);
console.log(elm);
var hdn = $(this).closest('tr').find('input[type="hidden"]').val();
console.log(hdn);
}
http://jsfiddle.net/bthorn/xf12y7y4/
我正在寻找的是这个
<input type="hidden" name="GridView1:_ctl2:hndTxtId" id="GridView1__ctl2_hndTxtId" value="3601">
我想要值3601,它在同一个tr
中HTML
<table>
<tr>
<td style="width:5px;">
<input type="hidden" name="GridView1:_ctl2:hndTxtId" id="GridView1__ctl2_hndTxtId" value="3601">
</td>
<td style="width:50px;"> <span id="GridView1__ctl2_lblVehicle0">413</span>
</td>
<td style="width:5px;">
<input type="button" id="GridView1__ctl2_AddButton0" name="GridView1:_ctl2:AddButton0" value="On" class="btn-blue" onclick="setDateTimeOn(this)">
</td>
<td style="width:150px;">
<span id="GridView1__ctl2_lblStormTimeOn"></span>
</td>
</tr>
我看到它吐出完整的输入元素标签但是var hdn,我尝试做一个console.log(hdn)并且它是未定义的。
答案 0 :(得分:2)
在这里,这应该适合你
var hdn = $(elm).closest('tr').find('input[type="hidden"]').val();
console.log(hdn);
答案 1 :(得分:2)
问题是您正在使用:
var hdn = $(this).closest('tr').find('input[type="hidden"]').val();
此处this
是全局Window对象。您想要使用元素:
var hdn = $(elm).closest('tr').find('input[type="hidden"]').val();