Fogeaters, Light The World.

14

2017-Oct

[jQuery] select box 제어

작성자: title: MoonBlonix IP ADRESS: *.64.228.3 조회 수: 1637

1. jQuery로 선택된 값 읽기

 

$("#selectBox option:selected").val();

$("select[name=name]").val();

 

2. jQuery로 선택된 내용 읽기

 

$("#selectBox option:selected").text();

 

3. 선택된 위치

 

var index = $("#test option").index($("#test option:selected"));

 

4. Add options to the end of a select

 

$("#selectBox").append("<option value='1'>Apples</option>");

$("#selectBox").append("<option value='2'>After Apples</option>");

 

5. Add options to the start of a select

 

$("#selectBox").prepend("<option value='0'>Before Apples</option>");

 

6. Replace all the options with new options

 

$("#selectBox").html("<option value='1'>Some oranges</option><option value='2'>MoreOranges</option>");

 

7. Replace items at a certain index

 

$("#selectBox option:eq(1)").replaceWith("<option value='2'>Someapples</option>");

$("#selectBox option:eq(2)").replaceWith("<option value='3'>Somebananas</option>");

 

8. 지정된 index값으로 select 하기

 

$("#selectBox option:eq(2)").attr("selected", "selected");

 

9. text 값으로 select 하기

 

$("#selectBox").val("Someoranges").attr("selected", "selected");

 

10. value값으로 select 하기

 

$("#selectBox").val("2");

 

11. 지정된 인덱스값의 item 삭제

 

$("#selectBox option:eq(0)").remove();

 

12. 첫번째 item 삭제

 

$("#selectBox option:first").remove();

 

13. 마지막 item 삭제

 

$("#selectBox option:last").remove();

 

14. 선택된 옵션의 text 구하기

 

alert(!$("#selectBox option:selected").text());

 

15. 선택된 옵션의 value 구하기

 

alert(!$("#selectBox option:selected").val());

 

16. 선택된 옵션 index 구하기

 

alert(!$("#selectBox option").index($("#selectBox option:selected")));

 

17. SelecBox 아이템 갯수 구하기

 

alert(!$("#selectBox option").size());

 

18. 선택된 옵션 앞의 아이템 갯수

 

alert(!$("#selectBox option:selected").prevAl!l().size());

 

19. 선택된 옵션 후의 아이템 갯수

 

alert(!$("#selectBox option:selected").nextAll().size());

 

20. Insert an item in after a particular position

 

$("#selectBox option:eq(0)").after("<option value='4'>Somepears</option>");

 

21. Insert an item in before a particular position

 

$("#selectBox option:eq(3)").before("<option value='5'>Someapricots</option>");

 

22. Getting values when item is selected

 

$("#selectBox").change(function(){

           alert(!$(this).val());

           alert(!$(this).children("option:selected").text());

});


출처 : http://blog.daum.net/twinsnow/124

profile
List of Articles
번호 제목 글쓴이 날짜 조회 수
공지 [Web] 클라우드 IDE + 2 title: MoonBlonix 2017-06-25 15125
132 [vsftpd] root(루트) 접속 title: MoonBlonix 2018-04-15 1733
131 [php] 모바일 인식 title: MoonBlonix 2018-03-19 1364
130 [nginx] php 502 Bad Gateway 에러 해결 title: MoonBlonix 2018-03-19 1416
129 라즈베리파이를 위한 nginx와 php7.0 저장소 title: MoonBlonix 2018-02-24 1470
128 [javascript] Date사용 및 날자 계산 title: MoonBlonix 2017-12-29 1619
127 [php] 달력 구현 소스 + 1 title: MoonBlonix 2017-12-25 1859
126 [javascript] 팝업 열기 title: MoonBlonix 2017-12-18 1728
125 [php] 날짜/시간 함수정리 title: MoonBlonix 2017-12-09 1583
124 [mysql] 조건문 사용 (if, case), (isnull, ifnull, nvl) title: MoonBlonix 2017-12-07 1361
123 [mysql] 서브쿼리(subquery) + 1 title: MoonBlonix 2017-12-07 1891
» [jQuery] select box 제어 title: MoonBlonix 2017-10-14 1637
121 [mysql] mysqli_fetch_array, mysqli_fetch_assoc 비교 title: MoonBlonix 2017-10-12 1478
120 [php] 조회수 중복방지 구현 + 1 title: MoonBlonix 2017-10-06 1573
119 [mysql] JOIN 사용 + 2 title: MoonBlonix 2017-10-04 1375
118 [DataBase] ORM(Object-Relational Mappings)에 대하여 title: MoonBlonix 2017-10-04 1342
117 CK에디터 파일(백업용) file title: MoonBlonix 2017-09-25 1550
116 [javascript] js, css 파일 동적 로딩 title: MoonBlonix 2017-09-20 1397
115 [web] 페이지 속도 개선 + 1 title: MoonBlonix 2017-09-17 1378
114 [jQuery] html 갈아엎기 title: MoonBlonix 2017-09-16 1618
113 [jQuery] 성능 최적화 title: MoonBlonix 2017-09-15 1859