리자

[펌] 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-09-12 조회 3,910
12-09-12 조회 2,904
12-09-12 조회 4,915
12-09-12 조회 4,580
12-09-12 조회 4,631
12-09-12 조회 3,794
12-09-12 조회 3,112
12-09-12 조회 6,370
12-09-12 조회 4,266
12-09-12 조회 4,594
12-09-12 조회 2,953
12-09-12 조회 2,895
12-09-11 조회 4,172
12-09-11 조회 3,009
12-09-11 조회 4,296
12-09-11 조회 3,941
12-09-11 조회 3,766
12-09-11 조회 3,733
12-09-11 조회 7,102
12-09-11 조회 5,730
12-09-11 조회 3,906
12-09-11 조회 2,809
12-09-11 조회 3,560
12-09-11 조회 4,011
12-09-11 조회 3,769
12-09-11 조회 3,544
12-09-11 조회 3,316
12-09-10 조회 2,865
12-09-10 조회 3,291
12-09-10 조회 3,138
12-09-10 조회 2,869
12-09-10 조회 2,818
12-09-10 조회 3,458
12-09-10 조회 2,753
12-09-10 조회 2,718
12-09-10 조회 2,754
12-09-10 조회 2,684
12-09-10 조회 3,525
12-09-10 조회 3,082
12-09-10 조회 4,690
12-09-10 조회 2,840
12-09-10 조회 3,111
12-09-10 조회 4,000
12-09-10 조회 3,044
12-09-10 조회 2,991
12-09-09 조회 2,924
12-09-09 조회 3,064
12-09-08 조회 3,519
12-09-08 조회 2,960
12-09-07 조회 3,122
12-09-07 조회 3,349
12-09-07 조회 3,171
12-09-07 조회 4,000
12-09-07 조회 3,287
12-09-07 조회 2,710
12-09-07 조회 3,377
12-09-07 조회 6,951
12-09-06 조회 3,023
12-09-06 조회 3,627
12-09-06 조회 5,370
12-09-06 조회 3,799
12-09-06 조회 4,168
12-09-06 조회 3,781
12-09-06 조회 4,570
12-09-06 조회 3,924
12-09-06 조회 3,484
12-09-06 조회 2,888
12-09-06 조회 1.6만
12-09-06 조회 3,973
12-09-06 조회 5,850
12-09-06 조회 3,518
12-09-06 조회 2,891
12-09-05 조회 3,059
12-09-05 조회 2,938
12-09-05 조회 3,100
12-09-05 조회 7,190
12-09-05 조회 8,902
12-09-05 조회 3,586
12-09-05 조회 7,463
12-09-05 조회 3,534
12-09-05 조회 2,969
12-09-05 조회 3,190
12-09-05 조회 3,014
12-09-05 조회 2,875
12-09-05 조회 3,160
12-09-05 조회 3,457
12-09-04 조회 2,926
12-09-04 조회 2,990
12-09-04 조회 3,569
12-09-04 조회 1만
12-09-04 조회 5,603
12-09-04 조회 2,949
12-09-04 조회 3,757
12-09-04 조회 3,569
12-09-04 조회 3,858
12-09-04 조회 7,663
12-09-04 조회 3,002
12-09-04 조회 3,603
12-09-03 조회 5,097
12-09-03 조회 4,722