◽ HTML & CSS & JS, jQuery

[jQuery - 기능 - (4) ] 체크박스에 여러값 넘기기 (구분자 사용)

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<html>
 
<form id="TestList123123123" name="TestList123123123" method="post" onsubmit="return false">
    <input type="text" value="123" id="abc" name="abc">
    <input type="text" value="456" id="abc2" name="abc2">
    <input type="text" value="789" id="abc3" name="abc3">
    <input type="checkbox" name="TestListName" value="A)B)C)D" id="">
</form>
 
 
<javascript>
 
    var formdata = $("#TestList123123123").serialize();
    
    jQuery.ajax({
        type :'post'
        , url : '/Test.do'
        , dataType : "json"
        , cache : false 
        , data : formdata                               
        , contentType: "application/x-www-form-urlencoded; charset=UTF-8"  
        , success:function(res){
 
        }
          , error:function(){
 
        }
    });
 
 
<java 서블릿>
//TestList = {"A)B)C)D", "E)E1)E2"}  대충 json형식으로 넘어오면 형식은 이렇다 >> {"abc": "123", "abc2": "456", "abc3": "789"}
String[] TestList = request.getParameterValues("TestListName");
 
for(int i=0; i<TestList.length;i++){ // lenth = 2 
    int index = TestList[i].indexOf(")"); //구분자는 마음대로해도 된다.
    String Test1 = TestList[i].substring(0, index); // Test1 = A
    String Test2 = TestList[i].substring(index+1);    // Test2 = B)C)D
}
 

 

 

문제는 Test2를 보면 그 다음 똑같은 인덱스의 위치를 잡고 싶지만 제공하는 함수가 없기 때문에
아래와 같이 수동적으로 만들어 줄 수 있다. 그러면 원하는 값들 다 뽑을 수 있다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
for(int i=0; i<moveIpAcco.length;i++){
    int index = moveIpAcco[i].indexOf(")");
    String test0 = moveIpAcco[i].substring(0, index);
        
    String moveIpAcco2 = moveIpAcco[i].substring(index+1);
    index = moveIpAcco2.indexOf(")"); // 두번째 ")"를 찾는 것이다.
    String test1 = moveIpAcco2.substring(0, index);
                
    String moveIpAcco3 = moveIpAcco2.substring(index+1);
    index = moveIpAcco3.indexOf(")"); // 세번째 ")"를 찾는 것이다.
    String test2 = moveIpAcco3.substring(0, index);
                
    String moveIpAcco4 = moveIpAcco3.substring(index+1);
    index = moveIpAcco4.indexOf(")"); // 네번째 ")"를 찾는 것이다.
    String test3 = moveIpAcco4.substring(0, index);
                
    String moveIpAcco5 = moveIpAcco4.substring(index+1);
    index = moveIpAcco5.indexOf(")"); // 다섯번째 ")"를 찾는 것이다.
    String test4 = moveIpAcco5.substring(0, index);
                
    String test5 = moveIpAcco5.substring(index+1); // 마지막은 이렇게 처리.
    }
 

푸터바