리자

[펌] 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,934
13년 전 조회 2,675
13년 전 조회 3,778
13년 전 조회 2,909
13년 전 조회 3,097
13년 전 조회 3,244
13년 전 조회 2,194
13년 전 조회 2,419
13년 전 조회 1.9만
13년 전 조회 3,580
13년 전 조회 5,587
13년 전 조회 2,856
13년 전 조회 3,341
13년 전 조회 3,271
13년 전 조회 1.7만
13년 전 조회 2,196
13년 전 조회 2,130
13년 전 조회 1,899
13년 전 조회 2,855
13년 전 조회 4,400
13년 전 조회 4,552
13년 전 조회 3,385
13년 전 조회 6,799
13년 전 조회 4,036
13년 전 조회 7,178
13년 전 조회 2,862
13년 전 조회 2.2만
13년 전 조회 2,116
13년 전 조회 2,632
13년 전 조회 1.8만
13년 전 조회 3,876
13년 전 조회 2,219
13년 전 조회 4,896
13년 전 조회 2,388
13년 전 조회 2,550
13년 전 조회 1,985
13년 전 조회 3,738
13년 전 조회 2,742
13년 전 조회 1,897
13년 전 조회 2,031
13년 전 조회 8,616
13년 전 조회 3,509
13년 전 조회 2,168
13년 전 조회 2,419
13년 전 조회 4,032
13년 전 조회 2,233
13년 전 조회 2만
13년 전 조회 4,095
13년 전 조회 3,914
13년 전 조회 5,980
13년 전 조회 3,170
13년 전 조회 2,004
13년 전 조회 2,201
13년 전 조회 1.7만
13년 전 조회 1,984
13년 전 조회 3,308
13년 전 조회 2,865
13년 전 조회 2,473
13년 전 조회 2,336
13년 전 조회 2,501
13년 전 조회 2.5만
13년 전 조회 3,706
13년 전 조회 3만
13년 전 조회 3,380
13년 전 조회 2.4만
13년 전 조회 6,827
13년 전 조회 2,105
13년 전 조회 2,099
13년 전 조회 3,923
13년 전 조회 2,258
13년 전 조회 3,461
13년 전 조회 2,634
13년 전 조회 2,579
13년 전 조회 7,839
13년 전 조회 6,411
13년 전 조회 2,242
13년 전 조회 2,320
13년 전 조회 6,199
13년 전 조회 7,543
13년 전 조회 2,315
13년 전 조회 1,668
13년 전 조회 2,050
13년 전 조회 3,086
13년 전 조회 6,762
13년 전 조회 2,197
13년 전 조회 2,911
13년 전 조회 2,484
13년 전 조회 1,628
13년 전 조회 2,469
13년 전 조회 1,921
13년 전 조회 1,787
13년 전 조회 7,277
13년 전 조회 2,049
13년 전 조회 1,581
13년 전 조회 3,928
13년 전 조회 2,257
13년 전 조회 4,668
13년 전 조회 2,455
13년 전 조회 2,122
13년 전 조회 1,678