리자

[펌] 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,751
13년 전 조회 1,470
13년 전 조회 2,498
13년 전 조회 1,648
13년 전 조회 1,912
13년 전 조회 1,999
13년 전 조회 1,071
13년 전 조회 1,260
13년 전 조회 1.8만
13년 전 조회 2,316
13년 전 조회 4,347
13년 전 조회 1,672
13년 전 조회 2,097
13년 전 조회 2,151
13년 전 조회 1.6만
13년 전 조회 1,068
13년 전 조회 927
13년 전 조회 718
13년 전 조회 1,610
13년 전 조회 3,190
13년 전 조회 3,445
13년 전 조회 2,114
13년 전 조회 5,648
13년 전 조회 2,804
13년 전 조회 6,021
13년 전 조회 1,628
13년 전 조회 2.1만
13년 전 조회 897
13년 전 조회 1,392
13년 전 조회 1.7만
13년 전 조회 2,777
13년 전 조회 1,022
13년 전 조회 3,733
13년 전 조회 1,186
13년 전 조회 1,338
13년 전 조회 785
13년 전 조회 2,516
13년 전 조회 1,509
13년 전 조회 653
13년 전 조회 899
13년 전 조회 7,462
13년 전 조회 2,397
13년 전 조회 934
13년 전 조회 1,304
13년 전 조회 2,873
13년 전 조회 1,028
13년 전 조회 1.9만
13년 전 조회 2,933
13년 전 조회 2,648
13년 전 조회 4,813
13년 전 조회 1,932
13년 전 조회 807
13년 전 조회 993
13년 전 조회 1.6만
13년 전 조회 807
13년 전 조회 2,209
13년 전 조회 1,766
13년 전 조회 1,299
13년 전 조회 1,160
13년 전 조회 1,293
13년 전 조회 2.3만
13년 전 조회 2,631
13년 전 조회 2.9만
13년 전 조회 2,199
13년 전 조회 2.2만
13년 전 조회 5,731
13년 전 조회 929
13년 전 조회 956
13년 전 조회 2,804
13년 전 조회 1,086
13년 전 조회 2,312
13년 전 조회 1,518
13년 전 조회 1,498
13년 전 조회 6,656
13년 전 조회 5,198
13년 전 조회 1,064
13년 전 조회 1,183
13년 전 조회 5,045
13년 전 조회 6,415
13년 전 조회 1,198
13년 전 조회 531
13년 전 조회 899
13년 전 조회 1,877
13년 전 조회 5,618
13년 전 조회 1,041
13년 전 조회 1,706
13년 전 조회 1,312
13년 전 조회 508
13년 전 조회 1,306
13년 전 조회 777
13년 전 조회 620
13년 전 조회 6,127
13년 전 조회 859
13년 전 조회 458
13년 전 조회 2,797
13년 전 조회 1,178
13년 전 조회 3,537
13년 전 조회 1,356
13년 전 조회 1,066
13년 전 조회 576
🐛 버그신고