如何获得子输入值;从其父div上的单击事件-
动态HTML
<div class="tableRowOdd">
<input type="hidden" value="28">
04/11/2012
</div>
JS
$('#historyContainer .tableRowOdd').live( 'click', function(e) {
// Here I want to get the hidden-input's value directly
// something like- $(this+'input').val()
console.log( $(this) );
});
不想从$(this).html()执行字符串操作
请帮帮忙。
发布于 2012-04-18 19:50:56
$('#historyContainer .tableRowOdd').live( 'click', function(e) {
console.log( $(this).find('input').val() ); // or .text()
});
请注意,这将遍历所有输入。如果你想要的只是第一个:
$('#historyContainer .tableRowOdd').live( 'click', function(e) {
console.log( $(this).find('input').first().val() ); // or .text()
});
发布于 2012-04-18 19:51:27
$(this).find('input').val()
应该返回值。
注意:如果您使用的是.on
1.7,或者在旧版本中使用.delegate
,则使用.on
incase。.live
不是首选函数。
在旧版本中使用.delegate
,
$('#historyContainer').delegate('.tableRowOdd', 'click', function(e) {
$(this).find('input').val()
console.log( $(this) );
});
使用.on
实现jQuery 1.7
$('#historyContainer').on('click', '.tableRowOdd', function(e) {
$(this).find('input').val()
console.log( $(this) );
});
发布于 2012-04-18 19:51:51
这可以通过jQuery实现:
$('#historyContainer .tableRowOdd').live( 'click', function(e) {
// if you have more inputs and wanna get the hidden
console.log( $(this).find('input[type="hidden"]').val() );
});
https://stackoverflow.com/questions/10216871
复制相似问题