■ checkbox로 선택된 값을 jquery를 이용하여 spring controller로 넘기는 예제이다. 선택된 값이 2개 이상 되지 않으면 넘기지 않고 선택된 값의 형제(?) 들의 값을 넘기고 spring controller에서 받는다.
▷ chkclass 클래스의 자식들 중에서 checked 된 것들을 모두 선택한다.
$(“.chkclass :checked”)
▷ chkclass 클래스의 자식들 중에서 checked 된 것들의 개수.
$(“.chkclass :checked”).size()
▷ chkclass 클래스의 자식들 중에서 id가 name인 것의 값.
$(this).parent().children(“#name”).val()
▷ id가 submitFrm인 버튼이 클릭되면 수행된다.
$(“#submitFrm”).on(“click”, function() {});
■ checkbox html
<button id="submitFrm">전송</button><br/><br/> <div class="chkclass"> <div> <input type="checkbox" id="chk" name="chk1" value="chkVal1"/>chkVal1 <input type="hidden" id="name" name="park" value="chkTxtVal1"/> <input type="hidden" id="age" value="23"/> </div> <div> <input type="checkbox" id="chk" name="chk2" value="chkVal2"/>chkVal2 <input type="hidden" id="name" name="nm2" value="chkTxtVal2"/> <input type="hidden" id="age" value="25"/> </div> <div> <input type="checkbox" id="chk" name="chk3" value="chkVal3"/>chkVal3 <input type="hidden" id="name" name="nm" value="chkTxtVal3"/> <input type="hidden" id="age" value="33"/> </div> <div> <input type="checkbox" id="chk" name="chk4" value="chkVal4"/>chkVal4 <input type="hidden" id="name" name="nm" value="chkTxtVal4"/> <input type="hidden" id="age" value="36"/> </div> <div> <input type="checkbox" id="chk" name="chk5" value="chkVal5"/>chkVal5 <input type="hidden" id="name" name="nm" value="chkTxtVal5"/> <input type="hidden" id="age" value="99"/> </div> </div>
■ jquery javascript
$(document).ready(function() { $("#submitFrm").on("click", function() { if ( $(".chkclass :checked").size()<2 ) { alert("체크 개수가 2개 이하입니다."); return; } else { var param = ""; $(".chkclass :checked").each(function() { if( param=="" ) param = "name="+$(this).parent().children("#name").val(); else param = param + "&name="+$(this).parent().children("#name").val(); param = param + "&age="+$(this).parent().children("#age").val(); }); $.ajax({ url : '/checkbox/updateChkBox', type : 'post', data : param, dataType : 'text', success : function(data) { console.log('return string : ' + data); }, error : function() { console.log('error');} }); } }); });
■ spring controller
▷ jquery client에서 name=chkTxtVal1&age=23&name=chkTxtVal2&age=25 값으로 spring controller에 전송되면 RequestParam 어노테이션을 이용하여(@RequestParam(value=”name”,required=true) List
@Controller("CheckboxController.class") @RequestMapping(value="/checkbox") public class CheckboxController { private static final Logger log = LoggerFactory.getLogger(CheckboxController.class); @RequestMapping(value="/view") public String view(HttpServletRequest request, SampleVO vo) throws Exception { return "/checkbox/view"; } @RequestMapping(value="/updateChkBox") public @ResponseBody String updateChkBox ( HttpServletRequest request, HttpServletResponse response, @RequestParam(value="name",required=true) List<String> name, @RequestParam(value="age",required=true) List<Integer> age) throws Exception { log.debug( ">>> param size : " + name.size() ); int i = 0; for( String value : name ){ log.debug( ">>> name's value : " + value + "\tage : " + age.get(i) ); i++; } return "success"; } }
- SpringBoot Port 변경 2022년 12월 27일
- jQuery checkbox 전체 선택/해제 예제 2022년 3월 15일
- Highcharts error #17 2019년 7월 10일
- Highchart TypeError: c.init is not a function 2019년 7월 10일
- Highchart 크기 조정 2019년 7월 8일
- Highcharts error #13 2019년 7월 8일
- Spring 트랜잭션 관리 2019년 6월 20일
- Spring RESTful 웹 서비스 사용하기 2019년 6월 19일
- Spring RESTful 웹 서비스 2019년 6월 19일
- Spring Scheduling Task 예제 2019년 6월 18일
- Grid Stack Two grids demo 예제 2019년 6월 12일
- 동적으로 추가/생성된 태그에 이벤트 추가 방법 2019년 6월 10일
- Highcharts update event 2019년 3월 26일
- spring mybatis mysql 연결 예제 2018년 5월 4일
- Spring Path Parameter 예제 2018년 2월 13일
- AngularJS $http, spring TEXT response 예제 2018년 1월 17일
- jdom2를 이용한 spring xml response(응답) 2017년 12월 5일
- jquery xml 요청, spring xml 리턴 2017년 12월 5일
- spring controller에서 get, post 동시 허용 방법 2017년 12월 5일
- mybatis, oracle procedure(오라클 커서)를 통한 데이터 조회 2017년 11월 6일
- HTTP 406 Not Acceptable in Spring, Jackson, Json 2017년 11월 6일
- jquery div show hide 예제 2016년 5월 25일
- jquery css 예제 2016년 5월 25일
- jquery selectbox 예제 2016년 5월 25일
- jquery json 예제 2016년 5월 25일
- jquery template 예제 1 2016년 5월 25일
- jquery datepicker 2016년 5월 9일
- ServletContextLister를 구현한 웹 애플리케이션 시작 시 작업 2016년 5월 2일
- jquery radiobox 2016년 4월 29일
- Web Application MessageResource 2016년 3월 8일
- org.mariadb.jdbc.internal.common.QueryException : No database selected 2016년 2월 19일
- jquery bgiframe 정리 2016년 2월 1일
- jquery each( $.each ) 2016년 1월 25일
- jquery spring – checkbox로 선택된 배열 주고 받기 2015년 8월 15일
- jquery spring ajax call 2015년 8월 15일
- spring json return 2015년 8월 15일
- spring exception – SimpleMappingExceptionResolver 2015년 8월 8일
- jQuery 동적 체크박스 제어 2015년 1월 27일
- 간단한 jquery chained selectbox 예제 2014년 12월 4일
- jquery ui tip 2014년 9월 2일