리자

[펌] 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개

댓글을 작성하시려면 로그인이 필요합니다.

프로그램

+
제목 글쓴이 날짜 조회
13년 전 조회 1만
13년 전 조회 2,857
13년 전 조회 3,933
13년 전 조회 3,063
13년 전 조회 3,273
13년 전 조회 3,392
13년 전 조회 2,316
13년 전 조회 2,527
13년 전 조회 1.9만
13년 전 조회 3,750
13년 전 조회 5,732
13년 전 조회 2,953
13년 전 조회 3,510
13년 전 조회 3,400
13년 전 조회 1.7만
13년 전 조회 2,314
13년 전 조회 2,298
13년 전 조회 2,058
13년 전 조회 3,020
13년 전 조회 4,557
13년 전 조회 4,643
13년 전 조회 3,528
13년 전 조회 6,913
13년 전 조회 4,190
13년 전 조회 7,277
13년 전 조회 3,004
13년 전 조회 2.2만
13년 전 조회 2,258
13년 전 조회 2,771
13년 전 조회 1.8만
13년 전 조회 3,988
13년 전 조회 2,376
13년 전 조회 5,008
13년 전 조회 2,567
13년 전 조회 2,710
13년 전 조회 2,150
13년 전 조회 3,917
13년 전 조회 2,896
13년 전 조회 2,045
13년 전 조회 2,151
13년 전 조회 8,735
13년 전 조회 3,623
13년 전 조회 2,314
13년 전 조회 2,530
13년 전 조회 4,149
13년 전 조회 2,370
13년 전 조회 2.1만
13년 전 조회 4,195
13년 전 조회 4,043
13년 전 조회 6,079
13년 전 조회 3,293
13년 전 조회 2,147
13년 전 조회 2,322
13년 전 조회 1.7만
13년 전 조회 2,134
13년 전 조회 3,411
13년 전 조회 2,964
13년 전 조회 2,602
13년 전 조회 2,486
13년 전 조회 2,643
13년 전 조회 2.5만
13년 전 조회 3,819
13년 전 조회 3만
13년 전 조회 3,508
13년 전 조회 2.4만
13년 전 조회 6,930
13년 전 조회 2,245
13년 전 조회 2,224
13년 전 조회 4,028
13년 전 조회 2,399
13년 전 조회 3,548
13년 전 조회 2,717
13년 전 조회 2,668
13년 전 조회 7,965
13년 전 조회 6,527
13년 전 조회 2,402
13년 전 조회 2,465
13년 전 조회 6,349
13년 전 조회 7,690
13년 전 조회 2,456
13년 전 조회 1,822
13년 전 조회 2,198
13년 전 조회 3,196
13년 전 조회 6,927
13년 전 조회 2,362
13년 전 조회 3,054
13년 전 조회 2,638
13년 전 조회 1,788
13년 전 조회 2,613
13년 전 조회 2,057
13년 전 조회 1,950
13년 전 조회 7,434
13년 전 조회 2,194
13년 전 조회 1,748
13년 전 조회 4,067
13년 전 조회 2,416
13년 전 조회 4,810
13년 전 조회 2,631
13년 전 조회 2,274
13년 전 조회 1,835