簡體   English   中英

如何在頁面加載時使用jQuery檢查復選框

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

我的問題是我想檢查頁面加載中的復選框。

這些復選框當前位於用戶表單的末尾,如下所示: 在此處輸入圖片說明

每個復選框對應一個規范對象。 在我的模型圖中,我添加了一個包含檢查值的“規范”列表,以及一個包含所有值的“ 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);

這是相應的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>

事實是,我正在顯示所有“ allspecification”列表,但我也希望檢查“ specification”列表中的元素。 如何實現此頁面加載?

提前致謝。

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

注意,我假設data變量包含Number類型的已檢查規范的ID。

在服務器端,您必須具有以下內容:

@Controller
public class A {

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

通過這樣做我解決了這個問題...

<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>

感謝所有人的答復。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM