I'm new to jsoup and having a bit of trouble with html tag. I need to get value attribute of select list options based on a text they contain. For example:
我是jsoup的新手,在html标签上有一点麻烦。我需要根据它们包含的文本获取选择列表选项的value属性。例如:
'<select id="list">
<option value="0">First value</option>
<option value="1">Second value</option>
<option value="2">Third value</option>
</select>'
Do you have an idea how I can tell jsoup to return the value "1" if it gets a "Second value" as a parameter for a search?
如果jsoup获得一个“第二个值”作为搜索参数,您知道如何让它返回值“1”吗?
4 个解决方案
#1
6
This may Help You..
这可以帮助你. .
String demo = "<select id='list'><option value='0'>First value</option><option value='1'>Second value</option><option value='2'>Third value</option></select>";
Document document = Jsoup.parse(demo);
Elements options = document.select("select > option");
for(Element element : options)
{
if(element.text().equalsIgnoreCase("second value"))
{
System.out.println(element.attr("value"));
}
}
#2
5
Here's another solution:
这是另一个解决方案:
public String searchAttribute(Element element, String str)
{
Elements lists = element.select("[id=list]");
for( Element e : lists )
{
Elements result = e.select("option:contains(" + str + ")");
if( !result.isEmpty() )
{
return result.first().attr("value");
}
}
return null;
}
Test:
测试:
Document doc = Jsoup.parse(html); // html is your listed html / xml
Strign result = searchAttribute(doc, "Second value") // result = 1
#3
2
Try this code:
试试这段代码:
String searchValue = "Second value";
Elements options = doc.select("#list > option");
String value = "";
for (Element option : options) {
String text = option.text();
if (text.equals(searchValue)){
value = option.attr("value");
}
}
Hope it helps!
希望它可以帮助!
#4
1
I think the simplest solution is:
我认为最简单的解决办法是:
Document doc = Jsoup.parse(html);
String value = doc.select("#list > option:contains(Second value)").val();
#1
6
This may Help You..
这可以帮助你. .
String demo = "<select id='list'><option value='0'>First value</option><option value='1'>Second value</option><option value='2'>Third value</option></select>";
Document document = Jsoup.parse(demo);
Elements options = document.select("select > option");
for(Element element : options)
{
if(element.text().equalsIgnoreCase("second value"))
{
System.out.println(element.attr("value"));
}
}
#2
5
Here's another solution:
这是另一个解决方案:
public String searchAttribute(Element element, String str)
{
Elements lists = element.select("[id=list]");
for( Element e : lists )
{
Elements result = e.select("option:contains(" + str + ")");
if( !result.isEmpty() )
{
return result.first().attr("value");
}
}
return null;
}
Test:
测试:
Document doc = Jsoup.parse(html); // html is your listed html / xml
Strign result = searchAttribute(doc, "Second value") // result = 1
#3
2
Try this code:
试试这段代码:
String searchValue = "Second value";
Elements options = doc.select("#list > option");
String value = "";
for (Element option : options) {
String text = option.text();
if (text.equals(searchValue)){
value = option.attr("value");
}
}
Hope it helps!
希望它可以帮助!
#4
1
I think the simplest solution is:
我认为最简单的解决办法是:
Document doc = Jsoup.parse(html);
String value = doc.select("#list > option:contains(Second value)").val();