리자

[펌] 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,029
13년 전 조회 4,104
13년 전 조회 3,234
13년 전 조회 3,460
13년 전 조회 3,565
13년 전 조회 2,385
13년 전 조회 2,593
13년 전 조회 1.9만
13년 전 조회 3,919
13년 전 조회 5,895
13년 전 조회 3,013
13년 전 조회 3,680
13년 전 조회 3,460
13년 전 조회 1.7만
13년 전 조회 2,377
13년 전 조회 2,472
13년 전 조회 2,251
13년 전 조회 3,197
13년 전 조회 4,737
13년 전 조회 4,711
13년 전 조회 3,701
13년 전 조회 6,973
13년 전 조회 4,362
13년 전 조회 7,330
13년 전 조회 3,178
13년 전 조회 2.2만
13년 전 조회 2,422
13년 전 조회 2,931
13년 전 조회 1.8만
13년 전 조회 4,051
13년 전 조회 2,555
13년 전 조회 5,063
13년 전 조회 2,738
13년 전 조회 2,884
13년 전 조회 2,335
13년 전 조회 4,094
13년 전 조회 3,057
13년 전 조회 2,209
13년 전 조회 2,203
13년 전 조회 8,790
13년 전 조회 3,677
13년 전 조회 2,469
13년 전 조회 2,578
13년 전 조회 4,211
13년 전 조회 2,527
13년 전 조회 2.1만
13년 전 조회 4,252
13년 전 조회 4,194
13년 전 조회 6,132
13년 전 조회 3,453
13년 전 조회 2,313
13년 전 조회 2,476
13년 전 조회 1.7만
13년 전 조회 2,272
13년 전 조회 3,470
13년 전 조회 3,025
13년 전 조회 2,745
13년 전 조회 2,647
13년 전 조회 2,808
13년 전 조회 2.5만
13년 전 조회 3,883
13년 전 조회 3만
13년 전 조회 3,695
13년 전 조회 2.4만
13년 전 조회 6,999
13년 전 조회 2,421
13년 전 조회 2,371
13년 전 조회 4,086
13년 전 조회 2,571
13년 전 조회 3,614
13년 전 조회 2,781
13년 전 조회 2,730
13년 전 조회 8,143
13년 전 조회 6,696
13년 전 조회 2,576
13년 전 조회 2,641
13년 전 조회 6,531
13년 전 조회 7,871
13년 전 조회 2,621
13년 전 조회 1,969
13년 전 조회 2,368
13년 전 조회 3,387
13년 전 조회 7,106
13년 전 조회 2,527
13년 전 조회 3,210
13년 전 조회 2,812
13년 전 조회 1,959
13년 전 조회 2,775
13년 전 조회 2,227
13년 전 조회 2,125
13년 전 조회 7,613
13년 전 조회 2,364
13년 전 조회 1,943
13년 전 조회 4,237
13년 전 조회 2,587
13년 전 조회 4,987
13년 전 조회 2,812
13년 전 조회 2,442
13년 전 조회 1,994