@Test public void usesOnForCheckboxValueIfNoValueSet() { Document doc = Jsoup.parse("<form><input type=checkbox checked name=foo></form>"); FormElement form = (FormElement) doc.select("form").first(); List<KeyVal> data = form.formData(); assertEquals("on", data.get(0).value()); assertEquals("foo", data.get(0).key()); }
public List<KeyVal> formData() { ArrayList<KeyVal> data = new ArrayList<KeyVal>(); for (Element el: elements) { if (!el.tag().isFormSubmittable()) continue; if (el.hasAttr("disabled")) continue; String name = el.attr("name"); if (name.length() == 0) continue; String type = el.attr("type"); if ("select".equals(el.tagName())) { Elements options = el.select("option[selected]"); boolean set = false; for (Element option: options) { data.add(KeyVal.create(name, option.val())); set = true; } if (!set) { Element option = el.select("option").first(); if (option != null) data.add(KeyVal.create(name, option.val())); } } else if ("checkbox".equalsIgnoreCase(type) || "radio".equalsIgnoreCase(type)) { if (el.hasAttr("checked")) { final String val = el.val().length() > 0 ? el.val() : "on"; data.add(KeyVal.create(name, val)); } } else { data.add(KeyVal.create(name, el.val())); } } return data; }
FormElement extends Element { public List<KeyVal> formData() { ArrayList<KeyVal> data = new ArrayList<KeyVal>(); for (Element el: elements) { if (!el.tag().isFormSubmittable()) continue; if (el.hasAttr("disabled")) continue; String name = el.attr("name"); if (name.length() == 0) continue; String type = el.attr("type"); if ("select".equals(el.tagName())) { Elements options = el.select("option[selected]"); boolean set = false; for (Element option: options) { data.add(KeyVal.create(name, option.val())); set = true; } if (!set) { Element option = el.select("option").first(); if (option != null) data.add(KeyVal.create(name, option.val())); } } else if ("checkbox".equalsIgnoreCase(type) || "radio".equalsIgnoreCase(type)) { if (el.hasAttr("checked")) { final String val = el.val().length() > 0 ? el.val() : "on"; data.add(KeyVal.create(name, val)); } } else { data.add(KeyVal.create(name, el.val())); } } return data; } }
FormElement extends Element { public List<KeyVal> formData() { ArrayList<KeyVal> data = new ArrayList<KeyVal>(); for (Element el: elements) { if (!el.tag().isFormSubmittable()) continue; if (el.hasAttr("disabled")) continue; String name = el.attr("name"); if (name.length() == 0) continue; String type = el.attr("type"); if ("select".equals(el.tagName())) { Elements options = el.select("option[selected]"); boolean set = false; for (Element option: options) { data.add(KeyVal.create(name, option.val())); set = true; } if (!set) { Element option = el.select("option").first(); if (option != null) data.add(KeyVal.create(name, option.val())); } } else if ("checkbox".equalsIgnoreCase(type) || "radio".equalsIgnoreCase(type)) { if (el.hasAttr("checked")) { final String val = el.val().length() > 0 ? el.val() : "on"; data.add(KeyVal.create(name, val)); } } else { data.add(KeyVal.create(name, el.val())); } } return data; } FormElement(Tag tag, String baseUri, Attributes attributes); }
FormElement extends Element { public List<KeyVal> formData() { ArrayList<KeyVal> data = new ArrayList<KeyVal>(); for (Element el: elements) { if (!el.tag().isFormSubmittable()) continue; if (el.hasAttr("disabled")) continue; String name = el.attr("name"); if (name.length() == 0) continue; String type = el.attr("type"); if ("select".equals(el.tagName())) { Elements options = el.select("option[selected]"); boolean set = false; for (Element option: options) { data.add(KeyVal.create(name, option.val())); set = true; } if (!set) { Element option = el.select("option").first(); if (option != null) data.add(KeyVal.create(name, option.val())); } } else if ("checkbox".equalsIgnoreCase(type) || "radio".equalsIgnoreCase(type)) { if (el.hasAttr("checked")) { final String val = el.val().length() > 0 ? el.val() : "on"; data.add(KeyVal.create(name, val)); } } else { data.add(KeyVal.create(name, el.val())); } } return data; } FormElement(Tag tag, String baseUri, Attributes attributes); Elements elements(); FormElement addElement(Element element); List<KeyVal> formData(); }
FormElement extends Element { public List<KeyVal> formData() { ArrayList<KeyVal> data = new ArrayList<KeyVal>(); for (Element el: elements) { if (!el.tag().isFormSubmittable()) continue; if (el.hasAttr("disabled")) continue; String name = el.attr("name"); if (name.length() == 0) continue; String type = el.attr("type"); if ("select".equals(el.tagName())) { Elements options = el.select("option[selected]"); boolean set = false; for (Element option: options) { data.add(KeyVal.create(name, option.val())); set = true; } if (!set) { Element option = el.select("option").first(); if (option != null) data.add(KeyVal.create(name, option.val())); } } else if ("checkbox".equalsIgnoreCase(type) || "radio".equalsIgnoreCase(type)) { if (el.hasAttr("checked")) { final String val = el.val().length() > 0 ? el.val() : "on"; data.add(KeyVal.create(name, val)); } } else { data.add(KeyVal.create(name, el.val())); } } return data; } FormElement(Tag tag, String baseUri, Attributes attributes); Elements elements(); FormElement addElement(Element element); List<KeyVal> formData();  }