我有一个HTML表格,如下所示:
<tr class="row-class" role="row">
<td>Text1</td>
<td>
<form method='get' action='http://example.php'>
<input type='hidden' name='id_num' value='ABCD123'> <!-- < I NEED THIS VALUE -->
<button type='submit' class='btn' title='Check' ></button>
</form>
</td>
</tr>
我想获取名为id_num
的隐藏输入类型的值。(在此示例中,我想要的值是“ABCD123”)。< / p>
我尝试用cheerio解析代码,如下所示:
var $ = cheerio.load(body);
$('tr').each(function(i, tr){
var children = $(this).children();
var x = children.eq(0);
var id_num = children.eq(1);
var row = {
"x": x.text().trim(), //this is correct, value is Text1
"id_num": id_num.text().trim() //This is empty, value is "", I want the value "ABCD123"
};
});
但我只能得到第一个正确的值。
如何从隐藏的输入元素id_num
中获取值?
感谢。
答案 0 :(得分:2)
修改强>
您的eq(1)
获得了整个<tr>
,请尝试以下方式:
$('tr').each(function(i, tr){
var children = $(this).children('td');
var x = $(children[0]);
var id_num = $(children[1]).find("input[name='id_num']");
var row = {
"x": x.text(),
"id_num": id_num.val()
};
}
答案 1 :(得分:1)
应该是:
$(tr).find('[name="id_num"]').attr('value')