리자

[펌] 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년 전 조회 3,089
13년 전 조회 4,168
13년 전 조회 3,304
13년 전 조회 3,533
13년 전 조회 3,628
13년 전 조회 2,401
13년 전 조회 2,615
13년 전 조회 1.9만
13년 전 조회 3,974
13년 전 조회 5,959
13년 전 조회 3,029
13년 전 조회 3,744
13년 전 조회 3,475
13년 전 조회 1.7만
13년 전 조회 2,395
13년 전 조회 2,553
13년 전 조회 2,320
13년 전 조회 3,280
13년 전 조회 4,808
13년 전 조회 4,728
13년 전 조회 3,781
13년 전 조회 6,988
13년 전 조회 4,429
13년 전 조회 7,347
13년 전 조회 3,257
13년 전 조회 2.2만
13년 전 조회 2,482
13년 전 조회 3,004
13년 전 조회 1.8만
13년 전 조회 4,074
13년 전 조회 2,613
13년 전 조회 5,080
13년 전 조회 2,797
13년 전 조회 2,942
13년 전 조회 2,407
13년 전 조회 4,168
13년 전 조회 3,137
13년 전 조회 2,282
13년 전 조회 2,213
13년 전 조회 8,813
13년 전 조회 3,698
13년 전 조회 2,537
13년 전 조회 2,591
13년 전 조회 4,236
13년 전 조회 2,596
13년 전 조회 2.1만
13년 전 조회 4,275
13년 전 조회 4,258
13년 전 조회 6,153
13년 전 조회 3,525
13년 전 조회 2,381
13년 전 조회 2,553
13년 전 조회 1.7만
13년 전 조회 2,348
13년 전 조회 3,491
13년 전 조회 3,036
13년 전 조회 2,815
13년 전 조회 2,727
13년 전 조회 2,877
13년 전 조회 2.5만
13년 전 조회 3,904
13년 전 조회 3만
13년 전 조회 3,774
13년 전 조회 2.4만
13년 전 조회 7,012
13년 전 조회 2,485
13년 전 조회 2,445
13년 전 조회 4,102
13년 전 조회 2,636
13년 전 조회 3,634
13년 전 조회 2,804
13년 전 조회 2,744
13년 전 조회 8,214
13년 전 조회 6,773
13년 전 조회 2,644
13년 전 조회 2,714
13년 전 조회 6,602
13년 전 조회 7,934
13년 전 조회 2,695
13년 전 조회 2,040
13년 전 조회 2,442
13년 전 조회 3,474
13년 전 조회 7,178
13년 전 조회 2,606
13년 전 조회 3,291
13년 전 조회 2,892
13년 전 조회 2,029
13년 전 조회 2,862
13년 전 조회 2,297
13년 전 조회 2,190
13년 전 조회 7,683
13년 전 조회 2,438
13년 전 조회 2,010
13년 전 조회 4,297
13년 전 조회 2,657
13년 전 조회 5,058
13년 전 조회 2,878
13년 전 조회 2,508
13년 전 조회 2,064