2012-06-08 67 views
0

我是新來的網頁開發和學習最多的「我走」。我在服務器端使用Spring-MVC,在客戶端使用JSP,JQuery和CSS。到目前爲止,我有一個簡單的頁面,其中包含2個時間選擇器(jQuery)和一個列表<div>s,每個包含項目數據和一個複選框。當點擊提交按鈕時,整個數據都以表格形式提交,並在我的Controller中收到@RequestParam-s。在我的下一步中,我希望每個<div>都具有一個複選框的功能:我實現了一個toggle函數,該函數更改背景並收集相關ID。現在我的問題是如何將這些數據提交給服務器(時間選擇器和數組ID)?我需要建立一個JSON對象並提交它嗎?我可以舉例說明如何從toggle函數中檢索var數組數據,並從input element獲取時間?
這裏是我的HTML片段:如何從JSP函數提交輸入元素和Ids數組中的數據?

<div class="container" > 
    <label class="TimeLabel" >Start:</label> 
    <input type="text" style="width: 3em;" id="timepicker_start" name="timepicker_start" value="09:00" /> 
    <label class="TimeLabel">End:</label> 
    <input type="text" style="width: 3em;" id="timepicker_end" name="timepicker_end" value="18:00" /> 
<div/> 
... 
<!-- item only list -->  
<div class="container"> 
    <ul class="plainList"> 
    <c:forEach items="${itemsFrom.itemsOnly}" var="item" varStatus="status">    
     <li> 
     <div class="inner"> 
      <img src="resources/images/${item.id}.png">   
      <ul class="plainList">    
      <li><h4>${item.title} &trade;</h4></li>    
      <li><h5>${item.description}</h5></li>    
      </ul>    
      <div style="clear: both"></div>   
     </div>   
     </li>   
    </c:forEach>  
    </ul>  
</div> 

和我的功能:

<script type="text/javascript"> 
    var count = 0; 
    var attrIds = new Array(); 
    $(function() { 
     $(".inner").toggle(function() { 
      $(this).css("background", "#2E8B57"); 
      attrIds.push(this.id); 
      count++; 
      $("section").find("#attrCount").text(count); 
     }, function() { 
      $(this).css("background", "#dcdcdc"); 
      attrIds.splice($.inArray(this.id, attrIds),1); 
      count--; 
      $("section").find("#attrCount").text(count); 
     }); 
    }); 
</script> 

回答

0

OK,所以我所做的是創建一個隱藏的元素:

 <script type="text/javascript"> 
    $(document).ready(function() { 
     var hidden = document.createElement("input"); 
     hidden.type = "hidden"; 
     hidden.name = "itemsIds"; 
     hidden.id = "itemsIds"; 
     document.forms[0].appendChild(hidden); 
    }); 
</script> 

而且在我加入的Toggle功能:

  var hid = document.getElementById("itemsIds"); 
     hid.value = attrIdsArr; 

現在,當我提交表格時,itemsIds被提交。 如果有人對我的解決方案及其效率有任何意見,我會很高興得到他們。

相關問題