리자

[펌] jQuery select box

· 2012-11-09 (금) 09:57:03 · 7468 · 1

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,382
13-07-14 조회 5,349
13-07-14 조회 4,625
13-07-14 조회 4,226
13-07-14 조회 4,070
13-07-14 조회 4,146
13-07-14 조회 4,869
13-07-14 조회 4,513
13-07-14 조회 4,291
13-07-14 조회 5,337
13-07-14 조회 5,216
13-07-14 조회 5,011
13-07-14 조회 4,715
13-07-14 조회 5,363
13-07-13 조회 3,560
13-07-01 조회 5,587
13-07-01 조회 5,753
13-07-01 조회 3,840
13-07-01 조회 5,190
13-05-27 조회 3,941
13-05-21 조회 4,840
13-05-02 조회 1만
13-04-29 조회 5,172
13-04-15 조회 5,439
13-04-13 조회 6,301
13-02-27 조회 3,817
13-02-26 조회 6,424
13-02-12 조회 4,996
13-02-01 조회 2.3만
13-01-23 조회 3,332
13-01-09 조회 7,977
13-01-07 조회 4,916
13-01-07 조회 3,614
13-01-04 조회 1.3만
13-01-03 조회 1.2만
12-12-31 조회 6,113
12-12-27 조회 4,419
12-12-27 조회 4,057
12-12-27 조회 8,689
12-12-18 조회 3,587
12-12-12 조회 3,657
12-12-12 조회 6,631
12-12-12 조회 8,110
12-12-05 조회 5,231
12-12-01 조회 6,634
12-11-22 조회 4,580
12-11-09 조회 5,394
12-11-09 조회 7,469
12-10-29 조회 4,735
12-10-16 조회 6,275
12-10-09 조회 5,242
12-09-02 조회 4,426
12-09-01 조회 3,519
12-08-30 조회 4,195
12-08-30 조회 4,175
12-08-28 조회 3,511
12-08-21 조회 7,131
12-08-20 조회 3,658
12-08-18 조회 5,050
12-08-18 조회 3,411
12-08-11 조회 3,564
12-08-09 조회 3,666
12-08-07 조회 3,600
12-08-07 조회 4,191
12-08-01 조회 4,537
12-08-01 조회 4,323
12-07-20 조회 9,816
12-07-17 조회 4,086
12-07-15 조회 3,883
12-07-10 조회 4,193
12-07-09 조회 6,905
12-07-05 조회 3,243
12-07-04 조회 3,409
12-06-30 조회 5,670
12-06-28 조회 3,740
12-06-20 조회 3,874
12-06-16 조회 3,128
12-06-14 조회 3,837
12-06-07 조회 3,824
12-06-06 조회 6,097
12-05-27 조회 4,374
12-05-25 조회 3,175
12-05-25 조회 2,948
12-05-24 조회 3,051
12-05-24 조회 4,504
12-05-23 조회 3,870
12-05-22 조회 6,479
12-05-15 조회 3,820
12-05-15 조회 3,611
12-05-14 조회 3,033
12-05-12 조회 3,504
12-05-11 조회 6,939
12-05-07 조회 4,407
12-04-28 조회 3,607
12-04-25 조회 5,221
12-04-15 조회 3,830
12-04-12 조회 4,667
12-04-11 조회 3,414
12-04-08 조회 3,198
12-03-21 조회 6,347