리자

[펌] 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,187건
+
제목 글쓴이 날짜 조회
12-06-21 조회 4,062
12-06-21 조회 3,597
12-06-20 조회 3,768
12-06-20 조회 4,315
12-06-20 조회 3,405
12-06-20 조회 4,751
12-06-20 조회 3,398
12-06-20 조회 3,351
12-06-20 조회 4,165
12-06-20 조회 4,386
12-06-18 조회 4,921
12-06-18 조회 3,575
12-06-18 조회 3,707
12-06-18 조회 3,498
12-06-18 조회 4,243
12-06-18 조회 4,898
12-06-17 조회 4,103
12-06-17 조회 4,020
12-06-17 조회 3,653
12-06-16 조회 3,453
12-06-15 조회 3,273
12-06-15 조회 3,973
12-06-15 조회 4,729
12-06-15 조회 4,451
12-06-14 조회 4,093
12-06-14 조회 4,108
12-06-14 조회 3,199
12-06-14 조회 3,608
12-06-14 조회 4,913
12-06-14 조회 3,397
12-06-14 조회 3,254
12-06-13 조회 7,474
12-06-13 조회 3,728
12-06-12 조회 5,707
12-06-11 조회 3,588
12-06-11 조회 3,593
12-06-11 조회 3,592
12-06-11 조회 3,490
12-06-11 조회 4,058
12-06-11 조회 3,369
12-06-08 조회 3,437
12-06-08 조회 4,929
12-06-08 조회 3,247
12-06-08 조회 5,006
12-06-08 조회 4,023
12-06-08 조회 3,556
12-06-08 조회 3,514
12-06-08 조회 4,536
12-06-08 조회 3,145
12-06-08 조회 3,827
12-06-07 조회 4,120
12-06-07 조회 4,013
12-06-06 조회 3,416
12-06-06 조회 3,500
12-06-06 조회 3,805
12-06-06 조회 6,480
12-06-06 조회 6,046
12-06-06 조회 6,793
12-06-05 조회 5,906
12-06-05 조회 3,325
12-06-05 조회 3,414
12-06-05 조회 3,429
12-06-05 조회 1.2만
12-06-05 조회 5,079
12-06-05 조회 5,582
12-06-05 조회 5,354
12-06-05 조회 5,694
12-06-05 조회 3,646
12-06-05 조회 3,578
12-06-05 조회 7,448
12-06-04 조회 3,413
12-06-04 조회 4,264
12-06-04 조회 3,980
12-06-04 조회 4,042
12-06-03 조회 3,890
12-06-02 조회 3,298
12-06-02 조회 4,016
12-06-01 조회 3,437
12-06-01 조회 4,191
12-06-01 조회 3,756
12-06-01 조회 3,383
12-06-01 조회 4,899
12-06-01 조회 3,756
12-06-01 조회 3,362
12-05-31 조회 5,053
12-05-31 조회 3,861
12-05-31 조회 3,788
12-05-31 조회 3,780
12-05-31 조회 3,592
12-05-30 조회 3,243
12-05-30 조회 3,292
12-05-30 조회 4,106
12-05-30 조회 3,354
12-05-29 조회 4,563
12-05-29 조회 3,889
12-05-28 조회 3,268
12-05-28 조회 6,718
12-05-28 조회 3,352
12-05-27 조회 4,680
12-05-27 조회 3,943