티스토리 뷰

Computer/Script Languages

[펌] jquery 유용한 팁

인생이글케쉬우냐 2014. 3. 29. 03:24

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
$(document).ready(function(){
});
 
 
// [ select box 컨트롤 ] --------------------------------------------------------------///
// 셀렉트박스 초기화        
//SelectBox 애완동물중 고양이 값 선택하기.
$("#ANI").val('2');
$("#ANI>option[value='2']").attr("selected", true);
//또는 옵션그룹이 있는Select Box 인경우
$("#ANI> optgroup >option[value='2']").attr("selected", true);
 
//선택된 값 읽어오기
$("#ANI option:selected").val();
$("select[name=ANI]").val();
 
//선택된 값 삭제
$('#ANI option:selected').remove();
 
//SelectBox 애완동물중 고양이 삭제
$("#ANI option[value='2']").remove();    
 
 
 
 
jQuery("#ElementID").is(":checked")
 
if(jQuery("#ElementID").is(":checked"))  
{
     체크된 상태
}
else
{
    체크가 해제된 상태
}
 
select box의 내용 가져오기
$("#select_box > option:selected").val();
select box의 값 설정
 $("#select_box > option[value=지정값]").attr("selected", "true")
select disabled 
$('#sign02_emp_id').attr('disabled', 'true');
해제 $('#sign02_emp_id').attr('disabled', ''); // 'false'를 줬을경우 안됨
기타 참고 : http://www.texotela.co.uk/code/jquery/select/
 
1) selelct box 내용 가져오기
$('#select_box > option:selected').val();
 
2) 값 설정
$('#select_box > option[value=지정값]').attr('selected', 'true');
 
3) select disabled
$('#select_box').attr('disabled', 'true');
 
 
 
 
 
 
// [ radio 컨트롤 ] --------------------------------------------------------------///
if($(':radio[name="section"]:checked').length < 1) { alert('분류를 선택하세요'); it.section[0].focus(); return false; } 
또는 if (!$(':radio[name="gender"]:checked').val()) { alert('성별을 선택하세요'); $("#gender1").select(); return false; } 
if($(':radio[name="p_addressGubun"]:checked').val() != 3) { ... }
체크해제 : $("#office").attr('checked', 'false')가 안되서 it.place[1].checked = false; 로 적용함
 
disabled : $(':radio[name="gender"]').attr('disabled', 'disabled');
 
$("input[name=radioName][value=" + resultValue + "]").attr("checked", true);
 
선택된 라디오 버튼 값 가져오기
$('input:radio[name="certWay"]').val();
$(':radio[name="radio"]:checked').val();
 
 
 
 
 
// [ checkedbox 컨트롤 ] --------------------------------------------------------------///
1. 개수 구하기
$("input[name=chk1]:checkbox:checked").length
 
☆☆☆  php에서 배열사용시  ☆☆☆
$("input[name='chk1[]']:checked").length
 
 
2. 체크여부 확인
$("#check_all").is(':checked')
 
 
3.  chk1 개수만큼 돌면서 실행한다
 
  $("input[name='item[]']:checkbox:checked").each(function(){items.push($(this).val());}); 
 
  var items_str = items.join(',');
 
  또는 var items_str = items.toString();
 
  또는 var items_str = $("input[name='item[]']:checkbox:checked").map(function () {return this.value;}).get();
 
  
4. disabled 하기
 
$("input[name=chk1]").attr('disabled', 'true');
 
 
1) checkbox checked 여부
id 인 경우 : $('input:checkbox [ id="checkbox_id" ]').is(":checked") == true;
name인 경우 : $('input:checkbox [ name="checkbox_name"]').is(":checked") == true;
 
2) checkbox 전체 갯수 : $('input:checkbox [ name="checkbox_name"]').length
3) checkbox 선택된 갯수 : $('input:checkbox [ name="checkbox_name"]:checked').length
 
4) checkbox 전체 순회 하며 checked 처리
$('input:checkbox [ name="checkbox_name"]').each(function() {
        this.checked = true;
        // if ( $(this).is(":checked") ) {
        //          처리로직;         
        // }
});
 
5) checkbox value 값 가져오기
$('input:checkbox [ id="checkbox_id" ]').val();
 
6) checkbox checked 처리하기
$('input:checkbox [ id="checkbox_id"]').attr("checked", true);
 
 $("#selectBox option[value='option1']").remove();
 
 // 체크박스 초기화
if( "${data.HOME}" == "Y" ) {
    $("#HOME").attr("checked", "checked");
}
if( "${data.GROUPW}" == "Y" ) {
    $("#GROUPW").attr("checked", "checked");
}
 
 
 
// 다이얼로그 띄우기
$("#crisCsFileSel").dialog({
    resizable    : false,
    autoOpen: false,
    height: 150,
    width: 500,
    modal: true,
    buttons: {
        "실행": function() {
            var selPath = crisCsFileSelFrm.crisCsSelPath.value;
            if( selPath == "" ) {
                alert("파일을 선택해주세요.");
            } else {
                crisSetCookie(crisPathName, selPath, 100);
                
                var ssoUrl  = "<%=PortalAgent.getSSOSyncUrl(sso_user_id, crisCode, true)%>";
                ssoUrl += "&cpath="+encodeURIComponent(selPath);
                
                window.open(ssoUrl, 'SNUHSSOPOPUP', '' );  
                $( this ).dialog( "close" );
                self.close();
            }
        },
        "취소": function() {
            $( this ).dialog( "close" );
            self.close();
            location.href = "about:blank";
        }
    }
});
 
$("#crisCsFileSel").dialog( "open" );
 
 
 
 
$.ajax({
    type     : "POST"
   ,url      : ""
   ,data     : {}        
   ,async    : false
   ,success  : function(data) {
       
    }
   ,error    : function(jqXHR, textStatus, errorThrown) {
       alert(jqXHR.status + " - " + jqXHR.statusText);
    }
});
 
 
 
// 폼 초기화 예 -----------------------------------------------------------------------///
 
// 생년월일 초기화
var birthday_year = "<%=birthday_year%>";
var birthday_month = "<%=birthday_month%>";
var birthday_day  = "<%=birthday_day%>";
if( birthday_year!="" ) { $("#BIRTHDAY_YEAR > option[value="+birthday_year+"]").attr("selected", "true"); }
if( birthday_month!="" ) { $("#BIRTHDAY_MONTH > option[value="+birthday_month+"]").attr("selected", "true"); }
if( birthday_day !="" ) { $("#BIRTHDAY_DAY > option[value="+birthday_day+"]").attr("selected", "true"); }
 
// 학년 초기화
var academic  = "<%=academic%>";
if( academic!="" ) { $("#ACADEMIC > option[value="+academic+"]").attr("selected", "true"); }
 
// 성별 초기화
var sex  = "<%=sex%>";
if( sex!="" ) { $("#ACADEMIC > option[value="+sex+"]").attr("selected", "true"); }
 
// 파트너 사이트 초기화
var partner_site  = "<%=partner_site%>";
if( partner_site!="" ) { $("#PARTNER_SITE > option[value="+partner_site+"]").attr("selected", "true"); }
 
// 경력 초기화
var experience             = "<%=experience%>";
var experience_year     = "<%=experience_year%>";
var experience_month      = "<%=experience_month%>";
$("input:radio[name=EXPERIENCE]").filter('input:radio[value='+experience+']').attr("checked", "checked");
if( experience=="2" ) { // 경력이라면...
    $("#EXPERIENCE_YEAR").removeAttr("disabled");
    $("#EXPERIENCE_MONTH").removeAttr("disabled");
    if( experience_year!="" ) { $("#EXPERIENCE_YEAR > option[value="+experience_year+"]").attr("selected", "true"); }
    if( experience_month!="" ) { $("#EXPERIENCE_MONTH > option[value="+experience_month+"]").attr("selected", "true"); }
}
 
 
 
// 년도 셀렉트박스 초기화
var today = new Date();
for( var i=today.getFullYear(); i>=2001; i-- ) {
    $('<option/>').attr('value', i).text(i).appendTo('#sb_year');
}
$('#sb_year option').first().attr('selected', 'selected');
 
 
 
 
strNos = strNos.replace(eval("/" + " " + "/gi"), "");
strNos = strNos.replace(eval("/" + "\"" + "/gi"), "");
strNos = strNos.replace(eval("/" + "\'" + "/gi"), "");
 
 
 
 
「자바스크립트 html제거」
 
1. 모든 태그 제거
str.replace(/<(\/)?([a-zA-Z]*)(\s[a-zA-Z]*=[^>]*)?(\s)*(\/)?>/g,"");
2. P태그 제거
str.replace(/<(\/)?[Pp](\/)?>/g,"");
3. BR태그 제거
tempStr.replace(/<br>/gi,"\n"); 

[출처] jquery 활용|작성자 주용준

반응형

'Computer > Script Languages' 카테고리의 다른 글

[Jquery] 플러그인  (0) 2010.02.01
[javascript] 숫자 유효성 검사  (2) 2009.06.05
javascript의 textarea 글자수 제한  (0) 2009.06.03
prototype.js  (0) 2009.05.14
Script / CSS + 한줄짜리 웹소스  (0) 2009.05.14