리자

[펌] 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,461
13-07-14 조회 5,422
13-07-14 조회 4,715
13-07-14 조회 4,320
13-07-14 조회 4,186
13-07-14 조회 4,240
13-07-14 조회 4,968
13-07-14 조회 4,646
13-07-14 조회 4,394
13-07-14 조회 5,430
13-07-14 조회 5,332
13-07-14 조회 5,097
13-07-14 조회 4,822
13-07-14 조회 5,508
13-07-13 조회 3,770
13-07-01 조회 5,775
13-07-01 조회 5,858
13-07-01 조회 3,959
13-07-01 조회 5,453
13-05-27 조회 4,018
13-05-21 조회 5,065
13-05-02 조회 1만
13-04-29 조회 5,261
13-04-15 조회 5,512
13-04-13 조회 6,447
13-02-27 조회 4,141
13-02-26 조회 6,534
13-02-12 조회 5,297
13-02-01 조회 2.3만
13-01-23 조회 3,580
13-01-09 조회 8,065
13-01-07 조회 4,976
13-01-07 조회 3,726
13-01-04 조회 1.3만
13-01-03 조회 1.2만
12-12-31 조회 6,416
12-12-27 조회 4,549
12-12-27 조회 4,232
12-12-27 조회 8,909
12-12-18 조회 3,722
12-12-12 조회 3,754
12-12-12 조회 6,791
12-12-12 조회 8,204
12-12-05 조회 5,496
12-12-01 조회 6,805
12-11-22 조회 4,715
12-11-09 조회 5,562
12-11-09 조회 7,629
12-10-29 조회 4,837
12-10-16 조회 6,385
12-10-09 조회 5,455
12-09-02 조회 4,565
12-09-01 조회 3,675
12-08-30 조회 4,336
12-08-30 조회 4,328
12-08-28 조회 3,669
12-08-21 조회 7,244
12-08-20 조회 3,767
12-08-18 조회 5,183
12-08-18 조회 3,555
12-08-11 조회 3,707
12-08-09 조회 3,877
12-08-07 조회 3,858
12-08-07 조회 4,436
12-08-01 조회 4,698
12-08-01 조회 4,588
12-07-20 조회 9,941
12-07-17 조회 4,304
12-07-15 조회 4,065
12-07-10 조회 4,332
12-07-09 조회 7,040
12-07-05 조회 3,380
12-07-04 조회 3,536
12-06-30 조회 5,772
12-06-28 조회 3,871
12-06-20 조회 4,030
12-06-16 조회 3,332
12-06-14 조회 3,982
12-06-07 조회 4,006
12-06-06 조회 6,368
12-05-27 조회 4,582
12-05-25 조회 3,333
12-05-25 조회 3,153
12-05-24 조회 3,271
12-05-24 조회 4,618
12-05-23 조회 3,996
12-05-22 조회 6,607
12-05-15 조회 4,141
12-05-15 조회 3,693
12-05-14 조회 3,127
12-05-12 조회 3,577
12-05-11 조회 7,079
12-05-07 조회 4,513
12-04-28 조회 3,713
12-04-25 조회 5,368
12-04-15 조회 3,944
12-04-12 조회 4,791
12-04-11 조회 3,550
12-04-08 조회 3,404
12-03-21 조회 6,449