3

Thymeleaf Spring 在使用带有复选框的表单时创建隐藏输入,根据http://www.thymeleaf.org/doc/html/Thymeleaf-Spring3.html#checkbox-fields

作为 GET 方法提交表单时生成的 url 有很多噪音:

/search?q=stuff&_filters=on&filters=Foo&_filters=on&filters=Bar&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on

我想看的网址是

/search?q=stuff&filters=Foo&filters=Bar

我怎样才能做到这一点?

4

2 回答 2

1

设法让它与拦截表单提交并删除所有隐藏的“_filters”输入的javascript hack一起工作:

function interceptFilterForm(form){
    var inputs = form.getElementsByTagName("input");

    for (var i = 0; i < inputs.length; ++i) {
        var input = inputs[i];
        if (input.type == "hidden" && input.name == "_filters") {
            input.parentNode.removeChild(input);
        }
    }

    form.submit();
}

表格:

<form th:action="@{/search}" method="GET" th:object="${searchForm}" onsubmit="interceptFilterForm(this);">
  <input type="hidden" name="q" th:value="${searchForm.q}" />
  <input type="submit" value="Filter" ></input>

  <li th:each="facet : ${results.facets}" >
    <input type="checkbox" name="filters" th:value="${facet.fullPath}" th:field="*{filters}" />
  </li>  
</form>
于 2013-09-12T15:18:02.553 回答
1

您可以使用后请求。问题是您需要绑定信息,否则您无法知道复选框是否未选中或未显示。

于 2013-09-13T06:58:46.423 回答