리자

[펌] 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,749
13년 전 조회 1,469
13년 전 조회 2,497
13년 전 조회 1,648
13년 전 조회 1,910
13년 전 조회 1,999
13년 전 조회 1,070
13년 전 조회 1,259
13년 전 조회 1.8만
13년 전 조회 2,315
13년 전 조회 4,347
13년 전 조회 1,672
13년 전 조회 2,095
13년 전 조회 2,149
13년 전 조회 1.6만
13년 전 조회 1,067
13년 전 조회 925
13년 전 조회 714
13년 전 조회 1,609
13년 전 조회 3,189
13년 전 조회 3,445
13년 전 조회 2,112
13년 전 조회 5,647
13년 전 조회 2,802
13년 전 조회 6,017
13년 전 조회 1,627
13년 전 조회 2.1만
13년 전 조회 895
13년 전 조회 1,389
13년 전 조회 1.7만
13년 전 조회 2,776
13년 전 조회 1,019
13년 전 조회 3,730
13년 전 조회 1,183
13년 전 조회 1,338
13년 전 조회 782
13년 전 조회 2,512
13년 전 조회 1,508
13년 전 조회 649
13년 전 조회 897
13년 전 조회 7,460
13년 전 조회 2,395
13년 전 조회 930
13년 전 조회 1,303
13년 전 조회 2,873
13년 전 조회 1,024
13년 전 조회 1.9만
13년 전 조회 2,930
13년 전 조회 2,646
13년 전 조회 4,812
13년 전 조회 1,929
13년 전 조회 807
13년 전 조회 989
13년 전 조회 1.6만
13년 전 조회 805
13년 전 조회 2,208
13년 전 조회 1,765
13년 전 조회 1,297
13년 전 조회 1,155
13년 전 조회 1,290
13년 전 조회 2.3만
13년 전 조회 2,627
13년 전 조회 2.9만
13년 전 조회 2,195
13년 전 조회 2.2만
13년 전 조회 5,727
13년 전 조회 925
13년 전 조회 953
13년 전 조회 2,800
13년 전 조회 1,083
13년 전 조회 2,306
13년 전 조회 1,516
13년 전 조회 1,493
13년 전 조회 6,655
13년 전 조회 5,193
13년 전 조회 1,060
13년 전 조회 1,182
13년 전 조회 5,041
13년 전 조회 6,414
13년 전 조회 1,196
13년 전 조회 528
13년 전 조회 895
13년 전 조회 1,876
13년 전 조회 5,614
13년 전 조회 1,038
13년 전 조회 1,702
13년 전 조회 1,310
13년 전 조회 505
13년 전 조회 1,299
13년 전 조회 774
13년 전 조회 618
13년 전 조회 6,126
13년 전 조회 855
13년 전 조회 457
13년 전 조회 2,794
13년 전 조회 1,171
13년 전 조회 3,533
13년 전 조회 1,352
13년 전 조회 1,063
13년 전 조회 572
🐛 버그신고