리자

[펌] 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개

댓글을 작성하시려면 로그인이 필요합니다.

프로그램

8,188건
+
제목 글쓴이 날짜 조회
13-03-03 조회 6,242
13-03-01 조회 5,570
13-03-01 조회 8,312
13-03-01 조회 6,308
13-02-26 조회 3,918
13-02-26 조회 3,861
13-02-26 조회 4,759
13-02-24 조회 8,171
13-02-24 조회 4,258
13-02-24 조회 3,500
13-02-24 조회 5,315
13-02-24 조회 5,308
13-02-24 조회 4,375
13-02-24 조회 5,712
13-02-24 조회 9,400
13-02-14 조회 5,107
13-02-13 조회 9,036
13-01-29 조회 7,679
13-01-24 조회 5,027
13-01-24 조회 4,902
13-01-17 조회 4,314
13-01-16 조회 4,345
13-01-08 조회 4,397
12-12-27 조회 4,059
12-12-20 조회 1.1만
12-12-20 조회 3,381
12-12-06 조회 4,369
12-12-01 조회 4,131
12-11-28 조회 5,698
12-11-25 조회 5,404
12-10-20 조회 4,665
12-10-03 조회 4,831
12-09-26 조회 4,991
12-09-15 조회 8,444
12-09-14 조회 5,458
12-09-11 조회 3,963
12-09-10 조회 3,978
12-09-05 조회 8,861
12-08-29 조회 4,660
12-08-26 조회 3,790
12-08-23 조회 3,489
12-08-22 조회 3,575
12-08-22 조회 4,388
12-08-21 조회 3,536
12-08-20 조회 3,455
12-08-20 조회 9,259
12-08-15 조회 4,378
12-08-13 조회 3,296
12-08-11 조회 4,719
12-08-05 조회 4,565
12-08-03 조회 3,346
12-08-03 조회 4,041
12-08-02 조회 5,023
12-07-29 조회 4,131
12-07-26 조회 3,708
12-07-26 조회 3,578
12-07-22 조회 3,489
12-07-20 조회 3,324
12-07-19 조회 4,188
12-07-19 조회 3,444
12-07-16 조회 3,564
12-07-16 조회 4,485
12-07-15 조회 3,559
12-07-12 조회 7,389
12-07-12 조회 5,032
12-07-11 조회 3,076
12-07-09 조회 3,240
12-07-05 조회 3,851
12-07-04 조회 4,422
12-07-04 조회 3,827
12-07-04 조회 4,349
12-06-29 조회 3,813
12-06-27 조회 3,402
12-06-24 조회 3,880
12-06-22 조회 4,189
12-06-22 조회 8,600
12-06-22 조회 3,251
12-06-21 조회 3,286
12-06-17 조회 3,911
12-06-14 조회 4,013
12-06-14 조회 3,294
12-06-08 조회 4,442
12-06-06 조회 3,403
12-06-04 조회 4,159
12-05-26 조회 4,977
12-05-26 조회 3,932
12-05-25 조회 3,228
12-05-24 조회 3,331
12-05-22 조회 3,375
12-05-20 조회 3,003
12-05-16 조회 3,138
12-05-15 조회 3,532
12-05-10 조회 3,963
12-05-08 조회 3,486
12-05-04 조회 3,339
12-05-03 조회 4,294
12-05-02 조회 3,429
12-05-02 조회 3,204
12-05-02 조회 3,302
12-04-26 조회 3,451