리자

[펌] 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,754
13년 전 조회 1,476
13년 전 조회 2,504
13년 전 조회 1,654
13년 전 조회 1,919
13년 전 조회 2,004
13년 전 조회 1,076
13년 전 조회 1,265
13년 전 조회 1.8만
13년 전 조회 2,324
13년 전 조회 4,353
13년 전 조회 1,677
13년 전 조회 2,102
13년 전 조회 2,157
13년 전 조회 1.6만
13년 전 조회 1,074
13년 전 조회 933
13년 전 조회 721
13년 전 조회 1,615
13년 전 조회 3,196
13년 전 조회 3,449
13년 전 조회 2,117
13년 전 조회 5,654
13년 전 조회 2,810
13년 전 조회 6,023
13년 전 조회 1,632
13년 전 조회 2.1만
13년 전 조회 904
13년 전 조회 1,398
13년 전 조회 1.7만
13년 전 조회 2,783
13년 전 조회 1,026
13년 전 조회 3,737
13년 전 조회 1,189
13년 전 조회 1,344
13년 전 조회 788
13년 전 조회 2,520
13년 전 조회 1,512
13년 전 조회 657
13년 전 조회 904
13년 전 조회 7,468
13년 전 조회 2,403
13년 전 조회 938
13년 전 조회 1,311
13년 전 조회 2,879
13년 전 조회 1,031
13년 전 조회 1.9만
13년 전 조회 2,938
13년 전 조회 2,652
13년 전 조회 4,822
13년 전 조회 1,936
13년 전 조회 812
13년 전 조회 997
13년 전 조회 1.6만
13년 전 조회 809
13년 전 조회 2,215
13년 전 조회 1,769
13년 전 조회 1,307
13년 전 조회 1,164
13년 전 조회 1,298
13년 전 조회 2.3만
13년 전 조회 2,634
13년 전 조회 2.9만
13년 전 조회 2,202
13년 전 조회 2.3만
13년 전 조회 5,738
13년 전 조회 934
13년 전 조회 956
13년 전 조회 2,809
13년 전 조회 1,089
13년 전 조회 2,316
13년 전 조회 1,522
13년 전 조회 1,501
13년 전 조회 6,659
13년 전 조회 5,201
13년 전 조회 1,068
13년 전 조회 1,190
13년 전 조회 5,051
13년 전 조회 6,420
13년 전 조회 1,203
13년 전 조회 535
13년 전 조회 902
13년 전 조회 1,881
13년 전 조회 5,621
13년 전 조회 1,047
13년 전 조회 1,710
13년 전 조회 1,317
13년 전 조회 508
13년 전 조회 1,308
13년 전 조회 780
13년 전 조회 624
13년 전 조회 6,137
13년 전 조회 862
13년 전 조회 462
13년 전 조회 2,801
13년 전 조회 1,181
13년 전 조회 3,541
13년 전 조회 1,358
13년 전 조회 1,069
13년 전 조회 581
🐛 버그신고