리자

[펌] 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,073
13년 전 조회 4,150
13년 전 조회 3,283
13년 전 조회 3,512
13년 전 조회 3,617
13년 전 조회 2,399
13년 전 조회 2,613
13년 전 조회 1.9만
13년 전 조회 3,962
13년 전 조회 5,939
13년 전 조회 3,028
13년 전 조회 3,726
13년 전 조회 3,473
13년 전 조회 1.7만
13년 전 조회 2,390
13년 전 조회 2,533
13년 전 조회 2,305
13년 전 조회 3,259
13년 전 조회 4,787
13년 전 조회 4,726
13년 전 조회 3,764
13년 전 조회 6,986
13년 전 조회 4,416
13년 전 조회 7,345
13년 전 조회 3,232
13년 전 조회 2.2만
13년 전 조회 2,466
13년 전 조회 2,987
13년 전 조회 1.8만
13년 전 조회 4,067
13년 전 조회 2,598
13년 전 조회 5,076
13년 전 조회 2,779
13년 전 조회 2,929
13년 전 조회 2,387
13년 전 조회 4,147
13년 전 조회 3,115
13년 전 조회 2,252
13년 전 조회 2,212
13년 전 조회 8,806
13년 전 조회 3,691
13년 전 조회 2,523
13년 전 조회 2,587
13년 전 조회 4,228
13년 전 조회 2,574
13년 전 조회 2.1만
13년 전 조회 4,273
13년 전 조회 4,240
13년 전 조회 6,151
13년 전 조회 3,512
13년 전 조회 2,362
13년 전 조회 2,530
13년 전 조회 1.7만
13년 전 조회 2,334
13년 전 조회 3,484
13년 전 조회 3,034
13년 전 조회 2,788
13년 전 조회 2,701
13년 전 조회 2,859
13년 전 조회 2.5만
13년 전 조회 3,899
13년 전 조회 3만
13년 전 조회 3,751
13년 전 조회 2.4만
13년 전 조회 7,011
13년 전 조회 2,467
13년 전 조회 2,427
13년 전 조회 4,098
13년 전 조회 2,620
13년 전 조회 3,630
13년 전 조회 2,802
13년 전 조회 2,741
13년 전 조회 8,192
13년 전 조회 6,756
13년 전 조회 2,622
13년 전 조회 2,699
13년 전 조회 6,587
13년 전 조회 7,920
13년 전 조회 2,675
13년 전 조회 2,024
13년 전 조회 2,418
13년 전 조회 3,457
13년 전 조회 7,160
13년 전 조회 2,583
13년 전 조회 3,273
13년 전 조회 2,867
13년 전 조회 2,006
13년 전 조회 2,838
13년 전 조회 2,278
13년 전 조회 2,174
13년 전 조회 7,669
13년 전 조회 2,419
13년 전 조회 1,997
13년 전 조회 4,283
13년 전 조회 2,636
13년 전 조회 5,046
13년 전 조회 2,869
13년 전 조회 2,489
13년 전 조회 2,046