리자

[펌] 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건
+
제목 글쓴이 날짜 조회
12-09-05 조회 7,430
12-08-31 조회 5,883
12-08-30 조회 6,647
12-08-22 조회 1.2만
12-08-09 조회 5,668
12-08-02 조회 3,746
12-07-30 조회 4,139
12-07-15 조회 3,753
12-07-10 조회 3,644
12-07-06 조회 3,473
12-07-06 조회 3,460
12-07-03 조회 4,059
12-07-03 조회 3,545
12-07-02 조회 3,438
12-06-25 조회 3,935
12-06-20 조회 3,310
12-06-15 조회 3,893
12-06-14 조회 3,166
12-06-08 조회 3,944
12-06-07 조회 3,937
12-06-05 조회 5,822
12-06-05 조회 3,542
12-06-04 조회 3,895
12-06-01 조회 4,092
12-06-01 조회 4,823
12-06-01 조회 3,689
12-05-30 조회 3,216
12-05-28 조회 3,194
12-05-23 조회 3,310
12-05-18 조회 3,211
12-05-16 조회 3,256
12-05-16 조회 3,228
12-05-16 조회 4,491
12-05-16 조회 3,014
12-05-16 조회 3,009
12-05-16 조회 3,393
12-05-16 조회 3,150
12-05-16 조회 3,048
12-05-15 조회 3,211
12-05-15 조회 2,896
12-05-15 조회 3,141
12-05-15 조회 3,578
12-05-15 조회 3,309
12-05-15 조회 2,686
12-05-14 조회 2,727
12-05-14 조회 2,899
12-05-14 조회 2,865
12-05-14 조회 3,172
12-05-14 조회 2,999
12-05-11 조회 3,291
12-05-11 조회 2,819
12-05-11 조회 2,721
12-05-11 조회 2,824
12-05-11 조회 3,395
12-05-11 조회 2,578
12-05-10 조회 3,980
12-05-10 조회 2,822
12-05-10 조회 2,759
12-05-10 조회 2,897
12-05-10 조회 4,500
12-05-10 조회 2,533
12-05-10 조회 2,631
12-05-10 조회 2,493
12-05-10 조회 2,748
12-05-10 조회 3,203
12-05-10 조회 3,491
12-05-10 조회 2,729
12-05-10 조회 2,848
12-05-10 조회 3,344
12-05-10 조회 2,939
12-05-08 조회 3,123
12-05-08 조회 2,939
12-05-08 조회 2,970
12-05-08 조회 3,354
12-05-08 조회 4,549
12-05-03 조회 2,925
12-05-03 조회 3,024
12-05-03 조회 2,797
12-05-03 조회 3,848
12-05-03 조회 2,894
12-05-02 조회 2,937
12-04-27 조회 2,833
12-04-27 조회 2,899
12-04-27 조회 2,809
12-04-26 조회 2,770
12-04-26 조회 3,128
12-04-26 조회 3,058
12-04-26 조회 2,722
12-04-26 조회 3,047
12-04-26 조회 3,657
12-04-26 조회 2,928
12-04-26 조회 3,592
12-04-26 조회 2,655
12-04-26 조회 2,993
12-04-26 조회 2,724
12-04-26 조회 2,969
12-04-26 조회 2,729
12-04-26 조회 3,156
12-04-26 조회 3,201
12-04-26 조회 3,019