리자

[펌] 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-02 조회 4,336
12-09-02 조회 4,725
12-09-02 조회 5,617
12-09-02 조회 3,057
12-09-02 조회 4,636
12-09-01 조회 3,746
12-08-31 조회 5,900
12-08-31 조회 5,935
12-08-30 조회 3,683
12-08-30 조회 6,718
12-08-30 조회 3,836
12-08-30 조회 3,979
12-08-30 조회 4,416
12-08-30 조회 4,424
12-08-29 조회 3,587
12-08-29 조회 3,664
12-08-29 조회 5,709
12-08-29 조회 3,604
12-08-29 조회 4,272
12-08-29 조회 5,342
12-08-29 조회 4,733
12-08-29 조회 3,423
12-08-29 조회 4,210
12-08-28 조회 6,542
12-08-28 조회 3,776
12-08-27 조회 4,118
12-08-26 조회 3,683
12-08-26 조회 3,857
12-08-26 조회 4,454
12-08-24 조회 3,316
12-08-24 조회 4,317
12-08-24 조회 4,044
12-08-23 조회 3,560
12-08-22 조회 1.2만
12-08-22 조회 3,633
12-08-22 조회 3,389
12-08-22 조회 3,566
12-08-22 조회 4,447
12-08-21 조회 3,652
12-08-21 조회 3,741
12-08-21 조회 3,508
12-08-21 조회 3,129
12-08-21 조회 7,317
12-08-21 조회 3,580
12-08-20 조회 3,888
12-08-20 조회 3,513
12-08-20 조회 9,325
12-08-20 조회 3,553
12-08-20 조회 3,851
12-08-19 조회 3,924
12-08-18 조회 5,249
12-08-18 조회 3,615
12-08-18 조회 5,466
12-08-18 조회 3,978
12-08-18 조회 4,530
12-08-17 조회 4,248
12-08-17 조회 4,314
12-08-17 조회 4,569
12-08-17 조회 3,181
12-08-16 조회 3,476
12-08-16 조회 3,545
12-08-16 조회 3,816
12-08-16 조회 3,805
12-08-15 조회 4,449
12-08-14 조회 3,485
12-08-13 조회 3,357
12-08-13 조회 5,769
12-08-13 조회 3,619
12-08-11 조회 3,923
12-08-11 조회 4,106
12-08-11 조회 3,776
12-08-11 조회 4,765
12-08-10 조회 3,460
12-08-09 조회 5,708
12-08-09 조회 3,936
12-08-08 조회 4,512
12-08-07 조회 3,594
12-08-07 조회 3,897
12-08-07 조회 4,288
12-08-07 조회 3,949
12-08-07 조회 4,527
12-08-07 조회 4,105
12-08-07 조회 3,551
12-08-06 조회 5,620
12-08-06 조회 3,769
12-08-05 조회 4,639
12-08-05 조회 3,689
12-08-05 조회 4,900
12-08-04 조회 4,674
12-08-03 조회 4,598
12-08-03 조회 3,406
12-08-03 조회 4,104
12-08-03 조회 3,721
12-08-03 조회 4,790
12-08-03 조회 3,593
12-08-03 조회 3,822
12-08-03 조회 3,890
12-08-03 조회 3,933
12-08-02 조회 3,601
12-08-02 조회 5,095