2012-08-26 108 views
1

我的問題是我想在頁面加載時檢查我的複選框。如何在頁面加載時使用jquery檢查複選框

這些複選框目前在用戶形式的端部,如下圖所示:enter image description here

每個複選框對應於規範對象。 在我modelmap,我加入一個包含檢查值的「規範」名單,以及包含所有值的「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> 

的事情是,我正在顯示所有「全部規格」列表,但我也希望檢查「規格」列表中的元素。 如何才能實現頁面加載?

在此先感謝。

回答

0
$(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; 
    } 
} 
+0

好的解決問題... – ASUR

0

通過這個幹什麼我解決售後服務問題...

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

感謝所有的答覆。

相關問題