리자

[펌] 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,912
13년 전 조회 1,612
13년 전 조회 2,663
13년 전 조회 1,812
13년 전 조회 2,059
13년 전 조회 2,150
13년 전 조회 1,228
13년 전 조회 1,423
13년 전 조회 1.8만
13년 전 조회 2,468
13년 전 조회 4,503
13년 전 조회 1,831
13년 전 조회 2,249
13년 전 조회 2,307
13년 전 조회 1.6만
13년 전 조회 1,230
13년 전 조회 1,077
13년 전 조회 865
13년 전 조회 1,753
13년 전 조회 3,341
13년 전 조회 3,570
13년 전 조회 2,260
13년 전 조회 5,812
13년 전 조회 2,958
13년 전 조회 6,159
13년 전 조회 1,779
13년 전 조회 2.1만
13년 전 조회 1,060
13년 전 조회 1,546
13년 전 조회 1.7만
13년 전 조회 2,922
13년 전 조회 1,178
13년 전 조회 3,892
13년 전 조회 1,344
13년 전 조회 1,486
13년 전 조회 928
13년 전 조회 2,664
13년 전 조회 1,655
13년 전 조회 804
13년 전 조회 1,052
13년 전 조회 7,614
13년 전 조회 2,546
13년 전 조회 1,080
13년 전 조회 1,458
13년 전 조회 3,022
13년 전 조회 1,168
13년 전 조회 1.9만
13년 전 조회 3,095
13년 전 조회 2,807
13년 전 조회 4,977
13년 전 조회 2,091
13년 전 조회 968
13년 전 조회 1,143
13년 전 조회 1.6만
13년 전 조회 929
13년 전 조회 2,358
13년 전 조회 1,907
13년 전 조회 1,441
13년 전 조회 1,309
13년 전 조회 1,448
13년 전 조회 2.4만
13년 전 조회 2,768
13년 전 조회 2.9만
13년 전 조회 2,352
13년 전 조회 2.3만
13년 전 조회 5,880
13년 전 조회 1,084
13년 전 조회 1,081
13년 전 조회 2,948
13년 전 조회 1,240
13년 전 조회 2,465
13년 전 조회 1,652
13년 전 조회 1,637
13년 전 조회 6,801
13년 전 조회 5,347
13년 전 조회 1,212
13년 전 조회 1,348
13년 전 조회 5,198
13년 전 조회 6,558
13년 전 조회 1,343
13년 전 조회 681
13년 전 조회 1,048
13년 전 조회 2,018
13년 전 조회 5,765
13년 전 조회 1,185
13년 전 조회 1,850
13년 전 조회 1,453
13년 전 조회 635
13년 전 조회 1,453
13년 전 조회 915
13년 전 조회 769
13년 전 조회 6,276
13년 전 조회 1,011
13년 전 조회 587
13년 전 조회 2,939
13년 전 조회 1,328
13년 전 조회 3,686
13년 전 조회 1,512
13년 전 조회 1,199
13년 전 조회 725
🐛 버그신고