简体   繁体   English

如何在Jsoup中发送复选框数据

[英]How do you send checkbox data in Jsoup

I am trying to post checkbox data with Jsoup and am having a little trouble. 我正在尝试使用Jsoup发布复选框数据,并且遇到了一些麻烦。 I thought that when multiple checkboxes are selected, they are sent as an array to the server but maybe that is not the case? 我以为选中多个复选框后,会将它们作为数组发送到服务器,但事实并非如此吗?

This is what I thought was correct: 我认为这是正确的:

HashMap<String, String> postData = new HashMap<String, String>();
postData.put("checkbox", "[box1,box2,box3]");

Jsoup.connect("somesite").data(postData).post();

This does not seem to work properly. 这似乎无法正常工作。 However, if I send only a single checkbox then I get my expected results leading me to believe my understanding of how checkbox form data sends is incorrect. 但是,如果仅发送一个复选框,则会得到预期的结果,使我相信我对复选框表单数据发送方式的理解是不正确的。

This works: 这有效:

postData.put("checkbox", "box2");

Maybe HashMap is the wrong type to use. 也许HashMap使用的类型错误。 According to the Jsoup documentation I could just call .data(key, value) multiple times but I was hoping for something a little cleaner than that. 根据Jsoup 文档,我可以多次调用.data(key,value),但是我希望有比这更干净的东西。

If you have multiple checkboxes, then presumably each checkbox has its own name attribute. 如果您有多个复选框,则大概每个复选框都有其自己的name属性。 You should then call .data(name, value) for each such name. 然后,您应该为每个这样的名称调用.data(name, value)

AFAIK there's no way to "collapse" these calls to data into a single call. AFAIK无法将这些对data调用“折叠”为单个调用。

Maybe You can try something like the following ? 也许您可以尝试以下方法?

 HashMap<String,String> paramHM=new HashMap<String,String>();

 ArrayList<String> checkboxVal=new ArrayList<Strnig>();
 / .. put request.getParametersValues() in this arraylist

 org.jsoup.Connection jsoupConn=Jsoup.connect(web_api).data(paramHM);

 // Multiple Call that 
 for(String item:checkboxVal){
    jsoupConn=jsoupConn.data("checkbox",item);
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM