리자

[펌] 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년 전 조회 1만
13년 전 조회 2,979
13년 전 조회 4,045
13년 전 조회 3,178
13년 전 조회 3,395
13년 전 조회 3,506
13년 전 조회 2,364
13년 전 조회 2,570
13년 전 조회 1.9만
13년 전 조회 3,854
13년 전 조회 5,833
13년 전 조회 2,992
13년 전 조회 3,622
13년 전 조회 3,442
13년 전 조회 1.7만
13년 전 조회 2,349
13년 전 조회 2,420
13년 전 조회 2,189
13년 전 조회 3,128
13년 전 조회 4,663
13년 전 조회 4,687
13년 전 조회 3,641
13년 전 조회 6,949
13년 전 조회 4,299
13년 전 조회 7,311
13년 전 조회 3,112
13년 전 조회 2.2만
13년 전 조회 2,364
13년 전 조회 2,865
13년 전 조회 1.8만
13년 전 조회 4,023
13년 전 조회 2,491
13년 전 조회 5,039
13년 전 조회 2,679
13년 전 조회 2,827
13년 전 조회 2,261
13년 전 조회 4,019
13년 전 조회 3,010
13년 전 조회 2,156
13년 전 조회 2,184
13년 전 조회 8,770
13년 전 조회 3,661
13년 전 조회 2,407
13년 전 조회 2,565
13년 전 조회 4,185
13년 전 조회 2,476
13년 전 조회 2.1만
13년 전 조회 4,234
13년 전 조회 4,140
13년 전 조회 6,111
13년 전 조회 3,392
13년 전 조회 2,234
13년 전 조회 2,414
13년 전 조회 1.7만
13년 전 조회 2,223
13년 전 조회 3,451
13년 전 조회 3,011
13년 전 조회 2,694
13년 전 조회 2,590
13년 전 조회 2,747
13년 전 조회 2.5만
13년 전 조회 3,863
13년 전 조회 3만
13년 전 조회 3,623
13년 전 조회 2.4만
13년 전 조회 6,975
13년 전 조회 2,344
13년 전 조회 2,310
13년 전 조회 4,065
13년 전 조회 2,501
13년 전 조회 3,584
13년 전 조회 2,758
13년 전 조회 2,700
13년 전 조회 8,070
13년 전 조회 6,626
13년 전 조회 2,495
13년 전 조회 2,568
13년 전 조회 6,454
13년 전 조회 7,798
13년 전 조회 2,560
13년 전 조회 1,907
13년 전 조회 2,301
13년 전 조회 3,306
13년 전 조회 7,035
13년 전 조회 2,469
13년 전 조회 3,145
13년 전 조회 2,749
13년 전 조회 1,889
13년 전 조회 2,714
13년 전 조회 2,166
13년 전 조회 2,055
13년 전 조회 7,541
13년 전 조회 2,296
13년 전 조회 1,862
13년 전 조회 4,157
13년 전 조회 2,517
13년 전 조회 4,915
13년 전 조회 2,731
13년 전 조회 2,371
13년 전 조회 1,924