리자

[펌] jQuery select box

jQuery로 선택된 값 읽기
$("#select_box option:selected").val();
$("select[name=name]").val();

jQuery로 선택된 내용 읽기
$("#select_box option:selected").text();

선택된 위치
var index = $("#test option").index($("#test option:selected")); 

-------------------------------------------------------------------

// Add options to the end of a select
$("#myselect").append("<option value='1'>Apples</option>");
$("#myselect").append("<option value='2'>After Apples</option>");

// Add options to the start of a select
$("#myselect").prepend("<option value='0'>Before Apples</option>");

// Replace all the options with new options
$("#myselect").html("<option value='1'>Some oranges</option><option value='2'>More Oranges</option><option value='3'>Even more oranges</option>");

// Replace items at a certain index
$("#myselect option:eq(1)").replaceWith("<option value='2'>Some apples</option>");
$("#myselect option:eq(2)").replaceWith("<option value='3'>Some bananas</option>");

// Set the element at index 2 to be selected
$("#myselect option:eq(2)").attr("selected", "selected");

// Set the selected element by text
$("#myselect").val("Some oranges").attr("selected", "selected");

// Set the selected element by value
$("#myselect").val("2");

// Remove an item at a particular index
$("#myselect option:eq(0)").remove();

// Remove first item
$("#myselect option:first").remove();

// Remove last item
$("#myselect option:last").remove();

// Get the text of the selected item
alert($("#myselect option:selected").text());

// Get the value of the selected item
alert($("#myselect option:selected").val());

// Get the index of the selected item
alert($("#myselect option").index($("#myselect option:selected")));

// Alternative way to get the selected item
alert($("#myselect option:selected").prevAll().size());

// Insert an item in after a particular position
$("#myselect option:eq(0)").after("<option value='4'>Some pears</option>");

// Insert an item in before a particular position
$("#myselect option:eq(3)").before("<option value='5'>Some apricots</option>");

// Getting values when item is selected
$("#myselect").change(function() {
alert($(this).val());
alert($(this).children("option:selected").text());
});





|

댓글 1개

댓글을 작성하시려면 로그인이 필요합니다.

프로그램

8,188건
+
제목 글쓴이 날짜 조회
13-07-14 조회 5,480
13-07-14 조회 5,443
13-07-14 조회 4,752
13-07-14 조회 4,344
13-07-14 조회 4,211
13-07-14 조회 4,261
13-07-14 조회 4,994
13-07-14 조회 4,704
13-07-14 조회 4,412
13-07-14 조회 5,454
13-07-14 조회 5,349
13-07-14 조회 5,114
13-07-14 조회 4,849
13-07-14 조회 5,531
13-07-13 조회 3,789
13-07-01 조회 5,804
13-07-01 조회 5,885
13-07-01 조회 3,983
13-07-01 조회 5,485
13-05-27 조회 4,050
13-05-21 조회 5,108
13-05-02 조회 1만
13-04-29 조회 5,281
13-04-15 조회 5,522
13-04-13 조회 6,456
13-02-27 조회 4,172
13-02-26 조회 6,554
13-02-12 조회 5,320
13-02-01 조회 2.4만
13-01-23 조회 3,601
13-01-09 조회 8,091
13-01-07 조회 5,004
13-01-07 조회 3,742
13-01-04 조회 1.3만
13-01-03 조회 1.2만
12-12-31 조회 6,447
12-12-27 조회 4,575
12-12-27 조회 4,263
12-12-27 조회 8,939
12-12-18 조회 3,752
12-12-12 조회 3,786
12-12-12 조회 6,812
12-12-12 조회 8,225
12-12-05 조회 5,528
12-12-01 조회 6,835
12-11-22 조회 4,761
12-11-09 조회 5,603
12-11-09 조회 7,650
12-10-29 조회 4,871
12-10-16 조회 6,407
12-10-09 조회 5,497
12-09-02 조회 4,593
12-09-01 조회 3,703
12-08-30 조회 4,371
12-08-30 조회 4,373
12-08-28 조회 3,707
12-08-21 조회 7,271
12-08-20 조회 3,800
12-08-18 조회 5,216
12-08-18 조회 3,581
12-08-11 조회 3,737
12-08-09 조회 3,905
12-08-07 조회 3,884
12-08-07 조회 4,467
12-08-01 조회 4,725
12-08-01 조회 4,610
12-07-20 조회 9,978
12-07-17 조회 4,327
12-07-15 조회 4,097
12-07-10 조회 4,364
12-07-09 조회 7,082
12-07-05 조회 3,418
12-07-04 조회 3,554
12-06-30 조회 5,804
12-06-28 조회 3,895
12-06-20 조회 4,070
12-06-16 조회 3,362
12-06-14 조회 4,011
12-06-07 조회 4,035
12-06-06 조회 6,392
12-05-27 조회 4,612
12-05-25 조회 3,354
12-05-25 조회 3,194
12-05-24 조회 3,291
12-05-24 조회 4,639
12-05-23 조회 4,024
12-05-22 조회 6,637
12-05-15 조회 4,156
12-05-15 조회 3,711
12-05-14 조회 3,150
12-05-12 조회 3,608
12-05-11 조회 7,101
12-05-07 조회 4,544
12-04-28 조회 3,736
12-04-25 조회 5,386
12-04-15 조회 3,972
12-04-12 조회 4,819
12-04-11 조회 3,578
12-04-08 조회 3,425
12-03-21 조회 6,477