简体   繁体   English

如何在页面加载时使用jQuery检查复选框

[英]How to check checkboxes using jquery on page load

My problem is that I want to check my checkboxes on page load. 我的问题是我想检查页面加载中的复选框。

These checkboxes are currently at the end of a user form, as shown below : 这些复选框当前位于用户表单的末尾,如下所示: 在此处输入图片说明

Each checkbox correspond to a Specification object. 每个复选框对应一个规范对象。 In my modelmap, I add a "specification" list containing the checked values, plus an "allspecification" list containing all the values : 在我的模型图中,我添加了一个包含检查值的“规范”列表,以及一个包含所有值的“ allspecification”列表:

List<Specification> specification=hot*******.get********otel(createHotel); //this is for check value.

List<Specification> allspecification=city****vice.getspec***fication(); //for all specification

map.addAttribute("orgspecification", specification);
map.addAttribute("allToatalspecfication", allspecification);

Here is the corresponding JSP page : 这是相应的JSP页面:

<div id="orgspecifications" style="margin-left:1px; height: 100px; width: 97%;">
  <c:forEach step="1" items="${allToatalspecfication}" var="specification">
    <div align="left" id="${specification.id}" style="width: 28%; color: #507B07; border: solid 1px gray; float: left; margin: 3px; height: 27px; background-color: #E9EBE3;">
      <input type="checkbox" name="checkbox" value="${specification.id}" id="checkbox" class="selectall"/>
      ${specification.name}
    </div>
  </c:forEach>
</div>

The thing is, I am showing all the "allspecification" list, but I also want the elements of the "specification" list to be checked. 事实是,我正在显示所有“ allspecification”列表,但我也希望检查“ specification”列表中的元素。 How can achieve this on page load ? 如何实现此页面加载?

Thanks in advance. 提前致谢。

$(function(){
  $.get('some/url',function(data){
    $('.selectall').each(function(){
      if ($.inArray(+$(this).val(),data) > -1){
        $(this).attr('checked',true);
      }
    });
  });
})

Note, I assume data variable contains checked specifications' ids of Number type. 注意,我假设data变量包含Number类型的已检查规范的ID。

On server-side you'll have to have something like this: 在服务器端,您必须具有以下内容:

@Controller
public class A {

  @RequestMapping('some/url')
  @ResponseBody
  public List<Integer> getIdsOfCheckedSpecs() {
    List<Integer> list = ...
    return list;
  }
}

by doin this i slove the issue... 通过这样做我解决了这个问题...

<div style="margin-top: 6px;">
                <div id="orgspecifications" style="margin-left:1px; height: 100px; width: 97%;">
<c:forEach step="1" items="${allToatalspecfication}"   var="specification">
                 <c:set value="${true}" var="unCheckFlag"></c:set>

                    <c:forEach  items="${orgspecification}" var="selectedspecification">

                                <c:if test="${specification.id eq selectedspecification.id}">
                                        <div align="left" id="${specification.id}" style="width: 28%; color: #507B07; border: solid 1px gray; float: left; margin: 3px; height: 27px; background-color: #E9EBE3;">
                                            <input type="checkbox" name="checkbox" value="${specification.id}" id="checkbox" class="selectall" checked="checked"/>
                                                ${specification.name}
                                        </div>
                                         <c:set value="${false}" var="unCheckFlag"></c:set>
                                </c:if>
                    </c:forEach>
                    <c:if test="${unCheckFlag == true}">

                                    <div align="left" id="${specification.id}" style="width: 28%; color: #507B07; border: solid 1px gray; float: left; margin: 3px; height: 27px; background-color: #E9EBE3;">
                                            <input type="checkbox" name="checkbox" value="${specification.id}" id="checkbox" class="selectall"/>
                                                ${specification.name}
                                    </div>
                   </c:if>      





</c:forEach>
</div>

Thanks to all for reply. 感谢所有人的答复。

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

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