[英]Get HTML nodes that have the same parent - JAVA
我有一個包含多種表單的文檔,類似於下面發布的示例。 我想從其中一個表單的隱藏輸入字段中提取所有名稱/值對,該表單由其名稱標識,我事先不知道將存在多少個隱藏字段。
我可以使用選擇器查詢選擇文檔中的所有相關輸入字段: input[type=hidden][name][value]
有沒有辦法只選擇具有FORM[name=lgo]
作為父級的輸入字段? 也許使用某種過濾器?
<FORM METHOD='POST' onSubmit='javascript:isWaitForm();' ACTION='https://abc-azerty.querty.se/carmon/servlet/action/change_1 ' name='lgo'>
<input type='hidden' name='LogInFlag' value='1'>
<input type='hidden' name='LogInTime' value='2011-07-26 11:10'>
<input type='hidden' name='cCode2' value='SE'>
<a href='javascript:isWaitForm();javascript:document.lgo.submit();' class='linkNone'>Business Monitor</a>
<a href='javascript:isWaitForm();javascript:document.lgo.submit();' class='linkNone'>
<input type='image' src='/images/button_arrow_right.gif' height=19 width=22 border=0 style='float:left;'></A>
</FORM>
基於此信息,至少以下一項應該有效 -
doc.select("form[name=lgo] > input[type=hidden]");
或者,您可以鏈接您的選擇 -
doc.select("form[name=lgo]").select("input[type=hidden]");
select 方法在文檔、元素或元素中可用。 它是上下文相關的,因此您可以通過從特定元素中進行選擇或通過鏈接選擇調用來進行過濾。
<script type="text/javascript">
var inputs = document.getElementsByName('lgo')[0].getElementsByTagName('input');
for(var i = 0 ; i < inputs.length ; i++){
if(inputs[i].getAttribute('type') == "hidden") {
// This will get the name: inputs[i].getAttribute('name')
// This will get the value: inputs[i].value)
console.log(inputs[i].getAttribute('name') + ": " + inputs[i].value);
}}
</script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.