리자

[펌] 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년 전 조회 3,098
13년 전 조회 4,180
13년 전 조회 3,319
13년 전 조회 3,545
13년 전 조회 3,639
13년 전 조회 2,407
13년 전 조회 2,622
13년 전 조회 1.9만
13년 전 조회 3,983
13년 전 조회 5,971
13년 전 조회 3,033
13년 전 조회 3,751
13년 전 조회 3,476
13년 전 조회 1.7만
13년 전 조회 2,399
13년 전 조회 2,567
13년 전 조회 2,337
13년 전 조회 3,296
13년 전 조회 4,826
13년 전 조회 4,732
13년 전 조회 3,798
13년 전 조회 6,992
13년 전 조회 4,443
13년 전 조회 7,351
13년 전 조회 3,267
13년 전 조회 2.2만
13년 전 조회 2,502
13년 전 조회 3,018
13년 전 조회 1.8만
13년 전 조회 4,077
13년 전 조회 2,629
13년 전 조회 5,082
13년 전 조회 2,815
13년 전 조회 2,957
13년 전 조회 2,423
13년 전 조회 4,188
13년 전 조회 3,151
13년 전 조회 2,295
13년 전 조회 2,213
13년 전 조회 8,818
13년 전 조회 3,703
13년 전 조회 2,553
13년 전 조회 2,592
13년 전 조회 4,240
13년 전 조회 2,609
13년 전 조회 2.1만
13년 전 조회 4,279
13년 전 조회 4,278
13년 전 조회 6,155
13년 전 조회 3,540
13년 전 조회 2,396
13년 전 조회 2,561
13년 전 조회 1.7만
13년 전 조회 2,356
13년 전 조회 3,496
13년 전 조회 3,037
13년 전 조회 2,828
13년 전 조회 2,746
13년 전 조회 2,894
13년 전 조회 2.5만
13년 전 조회 3,909
13년 전 조회 3만
13년 전 조회 3,787
13년 전 조회 2.4만
13년 전 조회 7,017
13년 전 조회 2,504
13년 전 조회 2,462
13년 전 조회 4,103
13년 전 조회 2,646
13년 전 조회 3,636
13년 전 조회 2,807
13년 전 조회 2,749
13년 전 조회 8,232
13년 전 조회 6,789
13년 전 조회 2,663
13년 전 조회 2,728
13년 전 조회 6,617
13년 전 조회 7,953
13년 전 조회 2,707
13년 전 조회 2,049
13년 전 조회 2,457
13년 전 조회 3,484
13년 전 조회 7,191
13년 전 조회 2,628
13년 전 조회 3,304
13년 전 조회 2,909
13년 전 조회 2,034
13년 전 조회 2,877
13년 전 조회 2,307
13년 전 조회 2,203
13년 전 조회 7,706
13년 전 조회 2,454
13년 전 조회 2,016
13년 전 조회 4,316
13년 전 조회 2,671
13년 전 조회 5,067
13년 전 조회 2,884
13년 전 조회 2,525
13년 전 조회 2,076