Script | Jquery select box 제어하기
페이지 정보
작성일2016-09-21 15:13 조회6,250회관련링크
본문
목차
1 기본 기능
1.a 선택값
1 2 3 | $( "#select_box option:selected" ).val(); $( "#select_box > option:selected" ).val() $( "select[name=name]" ).val(); |
1.b 선택 텍스트 내용
1 | $( "#select_box option:selected" ).text(); |
1.c 선택 위치(index)
1 | var index = $( "#test option" ).index($( "#test option:selected" )); |
2 추가 기능
2.a 마지막 자식요소 추가
1 2 3 | // Add options to the end of a select $( "#select_box" ).append( "<option value=" 1 ">black</option>" ); $( "#select_box" ).append( "<option value=" 2 ">white</option>" ); |
2.b 선택된 모든 요소의 앞에 추가하기
1 2 | // Add options to the start of a select $( "#select_box" ).prepend( "<option value=" 0 ">black</option>" ); |
2.c 전체 요소 추가하기
1 2 | // Replace all the options with new options $( "#select_box" ).html( "<option value=" 1 ">black</option><option value=" 2 ">white</option>" ); |
2.d 요소 바꾸기
1 2 3 | // Replace items at a certain index $( "#select_box option:eq(1)" ).replaceWith( "<option value=" 2 ">black</option>" ); $( "#select_box option:eq(2)" ).replaceWith( "<option value=" 3 ">white</option>" ); |
2.e SELECT 하기
1 2 3 4 5 6 7 8 9 10 | // 지정된 index 값으로 select 하기 $( "#select_box option:eq(2)" ).attr( "selected" , "selected" ); // text 값으로 select 하기 $( "#select_box" ).val( "Some oranges" ).attr( "selected" , "selected" ); // value 값으로 select 하기 $( "#select_box" ).val( "2" ); $( "#select_box > option[@value=지정값]" ).attr( "selected" , "true" );<font color= "#666666" ><span style= "white-space: normal;" > </span></font> |
2.f 삭제 하기
1 2 3 4 5 6 7 8 | // 지정된 인덱스 값의 item 삭제 $( "#select_box option:eq(0)" ).remove(); // 첫번째 item 삭제 $( "#select_box option:first" ).remove(); // 마지막 item 삭제 $( "#select_box option:last" ).remove(); |
2.g 선택값 구하기
1 2 3 4 5 6 7 8 9 10 11 | // 선택된 옵션의 text 구하기 alert($( "#select_box option:selected" ).text()); // 선택된 옵션의 value 구하기 alert($( "#select_box option:selected" ).val()); // 선택된 옵션 index 구하기 alert($( "#select_box option" ).index($( "#select_box option:selected" ))); // SelecBox 아이템 갯수 구하기 alert($( "#select_box option" ).size()); |
2.h 갯수 구하기
1 2 3 4 5 | // 선택된 옵션 앞의 아이템 갯수 alert($( "#select_box option:selected" ).prevAll().size()); // 선택된 옵션 후의 아이템 갯수 alert($( "#select_box option:selected" ).nextAll().size()); |
2.i 삽입하기
1 2 3 4 5 | // 0번째 item 다음에 삽입 $( "#select_box option:eq(0)" ).after( "<option value=" 1 ">black</option>" ); // 3번째 item 전에 삽입 $( "#select_box option:eq(3)" ).before( "<option value=" 2 ">white</option>" ); |
3 SELECT BOX 예제
1 2 3 4 5 6 | // select box 값이 변경될때 선택된 현재값 $( "#select_box" ).change( function () { alert($( this ).val()); alert($( this ).children( "option:selected" ).text()); });<font color= "#666666" ><span style= "white-space: normal;" > </span></font> |
3.a JQUERY UI SELECT BOX 예제
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 | <!doctype html> < html lang = "en" > < head > < meta charset = "utf-8" > < meta name = "viewport" content = "width=device-width, initial-scale=1" > < title >jQuery UI Selectmenu - Default functionality</ title > < link rel = "stylesheet" href = "//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" > < style > fieldset { border: 0; } label { display: block; margin: 30px 0 0 0; } .overflow { height: 200px; } </ style > < script > $( function() { $( "#speed" ).selectmenu(); $( "#files" ).selectmenu(); $( "#number" ) .selectmenu() .selectmenu( "menuWidget" ) .addClass( "overflow" ); $( "#salutation" ).selectmenu(); } ); </ script > </ head > < body > < div class = "demo" > < form action = "#" > < fieldset > < label for = "speed" >Select a speed</ label > < select name = "speed" id = "speed" > < option >Slower</ option > < option >Slow</ option > < option selected = "selected" >Medium</ option > < option >Fast</ option > < option >Faster</ option > </ select > < label for = "files" >Select a file</ label > < select name = "files" id = "files" > < optgroup label = "Scripts" > < option value = "jquery" >jQuery.js</ option > < option value = "jqueryui" >ui.jQuery.js</ option > </ optgroup > < optgroup label = "Other files" > < option value = "somefile" >Some unknown file</ option > < option value = "someotherfile" >Some other file with a very long option text</ option > </ optgroup > </ select > < label for = "number" >Select a number</ label > < select name = "number" id = "number" > < option >1</ option > < option selected = "selected" >2</ option > < option >3</ option > < option >4</ option > < option >5</ option > < option >6</ option > < option >7</ option > < option >8</ option > < option >9</ option > < option >10</ option > < option >11</ option > < option >12</ option > < option >13</ option > < option >14</ option > < option >15</ option > < option >16</ option > < option >17</ option > < option >18</ option > < option >19</ option > </ select > < label for = "salutation" >Select a title</ label > < select name = "salutation" id = "salutation" > < option disabled selected>Please pick one</ option > < option >Mr.</ option > < option >Mrs.</ option > < option >Dr.</ option > < option >Prof.</ option > < option >Other</ option > </ select > </ fieldset > </ form > </ div > </ body > </ html > |
Developer 관련 글 보기
- PHP | (PHP/MySQL) password_hash / password / old_password 2022-03-02
- PHP | 텔레그램(telegram) 봇 API 응답 값 살펴보기 2019-10-01
- PHP | [PHP] 텔레그램 api로 push 받기(Webhook) 2019-10-01
- Server | PHP 7.3, 7.2, 7.1 on CentOS/RHEL 6.10 2019-06-24
- Server | 사설인증서 생성을 위한 OpenSSL (for Windows) 사용방법 2019-06-14
- Script | jquery live, bind, delegate 를 on 대체 2018-01-16
- HTML | ASCII Code 특수기호 모음 2016-12-10