리자

[펌] jQuery select box

· 2012-11-09 (금) 09:57:03 · 7513 · 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건
+
제목 글쓴이 날짜 조회
12-11-22 조회 4,593
12-11-22 조회 6,133
12-11-22 조회 5,920
12-11-22 조회 5,134
12-11-22 조회 8,346
12-11-21 조회 5,852
12-11-21 조회 8,440
12-11-20 조회 4,657
12-11-20 조회 2.4만
12-11-19 조회 3,834
12-11-19 조회 4,318
12-11-19 조회 1.9만
12-11-18 조회 5,242
12-11-16 조회 3,930
12-11-16 조회 6,316
12-11-16 조회 4,080
12-11-16 조회 4,302
12-11-15 조회 3,719
12-11-15 조회 5,473
12-11-15 조회 4,355
12-11-15 조회 3,619
12-11-14 조회 3,380
12-11-14 조회 1만
12-11-14 조회 4,879
12-11-13 조회 3,885
12-11-13 조회 3,698
12-11-12 조회 5,484
12-11-12 조회 3,872
12-11-11 조회 2.2만
12-11-09 조회 5,426
12-11-09 조회 5,622
12-11-09 조회 7,514
12-11-08 조회 4,762
12-11-06 조회 3,677
12-11-06 조회 3,814
12-11-06 조회 1.9만
12-11-05 조회 3,643
12-11-05 조회 4,660
12-11-05 조회 4,198
12-11-05 조회 4,008
12-11-04 조회 3,964
12-11-03 조회 4,048
12-11-03 조회 2.6만
12-11-02 조회 5,048
12-11-02 조회 3.1만
12-11-01 조회 5,041
12-11-01 조회 2.6만
12-10-30 조회 8,223
12-10-30 조회 3,762
12-10-29 조회 3,694
12-10-29 조회 5,090
12-10-29 조회 3,921
12-10-29 조회 4,757
12-10-28 조회 3,988
12-10-26 조회 3,772
12-10-26 조회 9,761
12-10-26 조회 8,047
12-10-25 조회 3,920
12-10-25 조회 3,994
12-10-25 조회 8,090
12-10-24 조회 9,435
12-10-23 조회 3,997
12-10-23 조회 3,273
12-10-23 조회 3,608
12-10-23 조회 4,765
12-10-22 조회 8,403
12-10-21 조회 3,944
12-10-20 조회 4,583
12-10-19 조회 4,376
12-10-19 조회 3,253
12-10-18 조회 4,052
12-10-18 조회 3,591
12-10-18 조회 3,437
12-10-17 조회 9,269
12-10-17 조회 3,634
12-10-16 조회 3,329
12-10-16 조회 5,552
12-10-16 조회 3,990
12-10-16 조회 6,313
12-10-16 조회 4,181
12-10-15 조회 3,764
12-10-15 조회 3,391
12-10-15 조회 3,319
12-10-14 조회 3,586
12-10-13 조회 3,742
12-10-12 조회 4,575
12-10-12 조회 3,774
12-10-11 조회 4,963
12-10-11 조회 3,846
12-10-10 조회 6,967
12-10-10 조회 6,294
12-10-10 조회 7,868
12-10-10 조회 1만
12-10-10 조회 5,526
12-10-10 조회 4,984
12-10-10 조회 3,303
12-10-09 조회 5,778
12-10-09 조회 3,510
12-10-09 조회 7,188
12-10-09 조회 5,316