반응형


JQuery 선택자


1
2
3
4
5
6
7
8
9
10
11
12
<input type="text" class="text_box" id="text_id"/>
 
<select id="select_id">
    <option value="1">1</option>                    
    <option value="2">2</option>
</select>
 
<input type="radio" name="radio_name" value="1" checked="checked"/>
<input type="radio" name="radio_name" value="2"/>
 
<input type="checkbox" name="checkbox_name" id="checkbox_id1" value="1" checked="checked"/>
<input type="checkbox" name="checkbox_name" id="checkbox_id2" value="2"/>
cs


input type = "text" 일 경우

1
var text_value = $("#text_id").val();
cs
text_value 의 변수에 id가 "text_id"인 value 을 넣을 수 있다.

id 가 "text_id"인 곳에 값을 넣으려면 

1
$("#text_id").val('텍스트박스에 값넣기');
cs
text_id 의 value 가 '텍스트박스에 값넣기' 로 들어간다.



"select" 일 경우


1
var select_value = $("#select_id option:selected).val();
cs
cs

select_value 의 변수에 id가 "select_id"인 선택된 option의 value 를 넣을 수 있다.


1
$("#select_id").val('1').prop("selected",true);
cs


select_id 의 value 가 '1'인 option 이 선택된다.



input type = "radio" 일 경우


1
var radio_value = $(":input:radio[name=radio_name]:checked").val();
cs
radio_value 의 변수에 name이 "radio_name"인 요소중에 체크된 value 값이 들어간다.

요소 하나 하나 선택하고 싶을시에는 radio의 id 를 각각 주고 name 대신 id 로 선택하면된다.


1
$("input:radio[name='radio_name']:radio[value='2']").prop("checked",true);
cs
name 이 "radio_name" 인 요소 중에 value='2' 인 요소가 체크된다.


input type = "checkbox" 일 경우



1
$("input:checkbox[name='checkbox_name']:checkbox[value='2']").prop("checked",true);
cs
name 이 "checkbox_name" 인 요소 중에 value='2'인 요소가 체크된다.


1
$("#checkbox_id1").prop('checked',true);
cs
id 가 "checkbox_id1"인 요소가 체크된다.




반응형

+ Recent posts