문제

`<div class="col1">

<strong>
<select name="Category" multiple size="4">

<option value="A">A
<option value="B" selected>B
<option value="C">C
<option value="D">D

</select></strong>

</div>`

I have div class containing a drop down list as given above, i just need the value of 'selected' item from the drop down list using Jsoup

도움이 되었습니까?

해결책

Search for the <select> Element, iterate over it’s children and check if the selected attribute is present:

Document doc = Jsoup.parse("your html")
String selectedVal = null;
Elements options = doc.getElementsByAttributeValue("name", "Category").get(0).children();
for (Element option : options) {
    if (option.hasAttr("selected")) {
        selectedVal = option.val();
    }
}

Or in short with a CSS-like selector:

String selectedVal = doc.select("select[name=Category] option[selected]").val();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top