COMING SOON 🚀

리자

[펌] 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년 전 조회 9,989
13년 전 조회 2,716
13년 전 조회 3,817
13년 전 조회 2,945
13년 전 조회 3,149
13년 전 조회 3,277
13년 전 조회 2,256
13년 전 조회 2,469
13년 전 조회 1.9만
13년 전 조회 3,624
13년 전 조회 5,619
13년 전 조회 2,906
13년 전 조회 3,378
13년 전 조회 3,341
13년 전 조회 1.7만
13년 전 조회 2,258
13년 전 조회 2,172
13년 전 조회 1,937
13년 전 조회 2,890
13년 전 조회 4,442
13년 전 조회 4,603
13년 전 조회 3,418
13년 전 조회 6,852
13년 전 조회 4,070
13년 전 조회 7,234
13년 전 조회 2,896
13년 전 조회 2.2만
13년 전 조회 2,158
13년 전 조회 2,664
13년 전 조회 1.8만
13년 전 조회 3,933
13년 전 조회 2,259
13년 전 조회 4,961
13년 전 조회 2,428
13년 전 조회 2,591
13년 전 조회 2,025
13년 전 조회 3,784
13년 전 조회 2,783
13년 전 조회 1,940
13년 전 조회 2,102
13년 전 조회 8,679
13년 전 조회 3,570
13년 전 조회 2,206
13년 전 조회 2,483
13년 전 조회 4,091
13년 전 조회 2,274
13년 전 조회 2.1만
13년 전 조회 4,148
13년 전 조회 3,944
13년 전 조회 6,038
13년 전 조회 3,203
13년 전 조회 2,045
13년 전 조회 2,233
13년 전 조회 1.7만
13년 전 조회 2,023
13년 전 조회 3,364
13년 전 조회 2,910
13년 전 조회 2,511
13년 전 조회 2,381
13년 전 조회 2,536
13년 전 조회 2.5만
13년 전 조회 3,774
13년 전 조회 3만
13년 전 조회 3,422
13년 전 조회 2.4만
13년 전 조회 6,892
13년 전 조회 2,146
13년 전 조회 2,137
13년 전 조회 3,976
13년 전 조회 2,289
13년 전 조회 3,502
13년 전 조회 2,676
13년 전 조회 2,620
13년 전 조회 7,868
13년 전 조회 6,436
13년 전 조회 2,278
13년 전 조회 2,355
13년 전 조회 6,238
13년 전 조회 7,572
13년 전 조회 2,348
13년 전 조회 1,707
13년 전 조회 2,084
13년 전 조회 3,112
13년 전 조회 6,804
13년 전 조회 2,241
13년 전 조회 2,949
13년 전 조회 2,531
13년 전 조회 1,664
13년 전 조회 2,495
13년 전 조회 1,952
13년 전 조회 1,831
13년 전 조회 7,314
13년 전 조회 2,077
13년 전 조회 1,626
13년 전 조회 3,965
13년 전 조회 2,290
13년 전 조회 4,705
13년 전 조회 2,498
13년 전 조회 2,160
13년 전 조회 1,717