리자

[펌] 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년 전 조회 9,926
13년 전 조회 2,672
13년 전 조회 3,774
13년 전 조회 2,906
13년 전 조회 3,093
13년 전 조회 3,238
13년 전 조회 2,187
13년 전 조회 2,413
13년 전 조회 1.9만
13년 전 조회 3,577
13년 전 조회 5,581
13년 전 조회 2,848
13년 전 조회 3,339
13년 전 조회 3,263
13년 전 조회 1.7만
13년 전 조회 2,187
13년 전 조회 2,127
13년 전 조회 1,892
13년 전 조회 2,847
13년 전 조회 4,391
13년 전 조회 4,545
13년 전 조회 3,383
13년 전 조회 6,790
13년 전 조회 4,035
13년 전 조회 7,162
13년 전 조회 2,859
13년 전 조회 2.2만
13년 전 조회 2,109
13년 전 조회 2,625
13년 전 조회 1.8만
13년 전 조회 3,873
13년 전 조회 2,214
13년 전 조회 4,891
13년 전 조회 2,383
13년 전 조회 2,542
13년 전 조회 1,978
13년 전 조회 3,737
13년 전 조회 2,733
13년 전 조회 1,894
13년 전 조회 2,026
13년 전 조회 8,614
13년 전 조회 3,504
13년 전 조회 2,165
13년 전 조회 2,417
13년 전 조회 4,026
13년 전 조회 2,229
13년 전 조회 2만
13년 전 조회 4,091
13년 전 조회 3,906
13년 전 조회 5,973
13년 전 조회 3,166
13년 전 조회 2,001
13년 전 조회 2,199
13년 전 조회 1.7만
13년 전 조회 1,983
13년 전 조회 3,300
13년 전 조회 2,862
13년 전 조회 2,467
13년 전 조회 2,333
13년 전 조회 2,498
13년 전 조회 2.5만
13년 전 조회 3,698
13년 전 조회 3만
13년 전 조회 3,380
13년 전 조회 2.4만
13년 전 조회 6,823
13년 전 조회 2,103
13년 전 조회 2,094
13년 전 조회 3,918
13년 전 조회 2,255
13년 전 조회 3,456
13년 전 조회 2,632
13년 전 조회 2,576
13년 전 조회 7,838
13년 전 조회 6,410
13년 전 조회 2,238
13년 전 조회 2,317
13년 전 조회 6,195
13년 전 조회 7,540
13년 전 조회 2,310
13년 전 조회 1,664
13년 전 조회 2,046
13년 전 조회 3,083
13년 전 조회 6,757
13년 전 조회 2,195
13년 전 조회 2,908
13년 전 조회 2,478
13년 전 조회 1,624
13년 전 조회 2,463
13년 전 조회 1,917
13년 전 조회 1,783
13년 전 조회 7,271
13년 전 조회 2,043
13년 전 조회 1,575
13년 전 조회 3,924
13년 전 조회 2,252
13년 전 조회 4,665
13년 전 조회 2,452
13년 전 조회 2,120
13년 전 조회 1,675