리자

[펌] 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년 전 조회 8,997
13년 전 조회 1,718
13년 전 조회 2,756
13년 전 조회 1,895
13년 전 조회 2,143
13년 전 조회 2,248
13년 전 조회 1,319
13년 전 조회 1,507
13년 전 조회 1.8만
13년 전 조회 2,566
13년 전 조회 4,589
13년 전 조회 1,925
13년 전 조회 2,335
13년 전 조회 2,411
13년 전 조회 1.6만
13년 전 조회 1,312
13년 전 조회 1,178
13년 전 조회 966
13년 전 조회 1,864
13년 전 조회 3,434
13년 전 조회 3,650
13년 전 조회 2,368
13년 전 조회 5,908
13년 전 조회 3,046
13년 전 조회 6,266
13년 전 조회 1,876
13년 전 조회 2.1만
13년 전 조회 1,153
13년 전 조회 1,640
13년 전 조회 1.7만
13년 전 조회 3,021
13년 전 조회 1,266
13년 전 조회 3,989
13년 전 조회 1,435
13년 전 조회 1,576
13년 전 조회 1,028
13년 전 조회 2,766
13년 전 조회 1,741
13년 전 조회 901
13년 전 조회 1,145
13년 전 조회 7,713
13년 전 조회 2,642
13년 전 조회 1,180
13년 전 조회 1,536
13년 전 조회 3,111
13년 전 조회 1,272
13년 전 조회 2만
13년 전 조회 3,178
13년 전 조회 2,908
13년 전 조회 5,066
13년 전 조회 2,192
13년 전 조회 1,060
13년 전 조회 1,235
13년 전 조회 1.6만
13년 전 조회 1,007
13년 전 조회 2,443
13년 전 조회 1,997
13년 전 조회 1,537
13년 전 조회 1,417
13년 전 조회 1,543
13년 전 조회 2.4만
13년 전 조회 2,866
13년 전 조회 2.9만
13년 전 조회 2,459
13년 전 조회 2.3만
13년 전 조회 5,987
13년 전 조회 1,185
13년 전 조회 1,167
13년 전 조회 3,043
13년 전 조회 1,342
13년 전 조회 2,565
13년 전 조회 1,763
13년 전 조회 1,745
13년 전 조회 6,892
13년 전 조회 5,448
13년 전 조회 1,305
13년 전 조회 1,442
13년 전 조회 5,295
13년 전 조회 6,655
13년 전 조회 1,439
13년 전 조회 768
13년 전 조회 1,156
13년 전 조회 2,123
13년 전 조회 5,851
13년 전 조회 1,286
13년 전 조회 1,955
13년 전 조회 1,550
13년 전 조회 734
13년 전 조회 1,561
13년 전 조회 1,012
13년 전 조회 860
13년 전 조회 6,376
13년 전 조회 1,121
13년 전 조회 672
13년 전 조회 3,028
13년 전 조회 1,418
13년 전 조회 3,786
13년 전 조회 1,613
13년 전 조회 1,276
13년 전 조회 831