리자

[펌] 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,547
13년 전 조회 2,435
13년 전 조회 3,505
13년 전 조회 2,678
13년 전 조회 2,836
13년 전 조회 3,009
13년 전 조회 1,885
13년 전 조회 2,071
13년 전 조회 1.8만
13년 전 조회 3,321
13년 전 조회 5,356
13년 전 조회 2,520
13년 전 조회 3,105
13년 전 조회 2,984
13년 전 조회 1.7만
13년 전 조회 1,879
13년 전 조회 1,872
13년 전 조회 1,661
13년 전 조회 2,655
13년 전 조회 4,164
13년 전 조회 4,239
13년 전 조회 3,153
13년 전 조회 6,479
13년 전 조회 3,817
13년 전 조회 6,871
13년 전 조회 2,634
13년 전 조회 2.2만
13년 전 조회 1,872
13년 전 조회 2,411
13년 전 조회 1.8만
13년 전 조회 3,571
13년 전 조회 1,957
13년 전 조회 4,580
13년 전 조회 2,145
13년 전 조회 2,287
13년 전 조회 1,746
13년 전 조회 3,494
13년 전 조회 2,465
13년 전 조회 1,635
13년 전 조회 1,714
13년 전 조회 8,296
13년 전 조회 3,191
13년 전 조회 1,940
13년 전 조회 2,106
13년 전 조회 3,773
13년 전 조회 1,996
13년 전 조회 2만
13년 전 조회 3,838
13년 전 조회 3,647
13년 전 조회 5,715
13년 전 조회 2,925
13년 전 조회 1,755
13년 전 조회 1,957
13년 전 조회 1.7만
13년 전 조회 1,715
13년 전 조회 3,018
13년 전 조회 2,580
13년 전 조회 2,222
13년 전 조회 2,099
13년 전 조회 2,254
13년 전 조회 2.4만
13년 전 조회 3,425
13년 전 조회 3만
13년 전 조회 3,175
13년 전 조회 2.3만
13년 전 조회 6,532
13년 전 조회 1,855
13년 전 조회 1,853
13년 전 조회 3,629
13년 전 조회 2,041
13년 전 조회 3,251
13년 전 조회 2,338
13년 전 조회 2,289
13년 전 조회 7,608
13년 전 조회 6,176
13년 전 조회 2,003
13년 전 조회 2,110
13년 전 조회 5,972
13년 전 조회 7,323
13년 전 조회 2,086
13년 전 조회 1,442
13년 전 조회 1,834
13년 전 조회 2,855
13년 전 조회 6,554
13년 전 조회 1,955
13년 전 조회 2,673
13년 전 조회 2,241
13년 전 조회 1,381
13년 전 조회 2,228
13년 전 조회 1,685
13년 전 조회 1,511
13년 전 조회 7,039
13년 전 조회 1,804
13년 전 조회 1,350
13년 전 조회 3,705
13년 전 조회 2,045
13년 전 조회 4,469
13년 전 조회 2,214
13년 전 조회 1,893
13년 전 조회 1,443