IT/jquery_javascript

jquery select selector

당양부부34 2013. 8. 23. 15:54

1. jQuery로 선택된 값 읽기

$("#selectBoxoption:selected").val();

$("select[name=name]").val();

2. jQuery로 선택된 내용 읽기

$("#selectBoxoption:selected").text();

3. 선택된 위치

var index =$("#test option").index($("#test option:selected"));

4. Addoptions to the end of a select

$("#selectBox").append("<optionvalue='1'>Apples</option>");

$("#selectBox").append("<optionvalue='2'>After Apples</option>");

5. Addoptions to the start of a select

$("#selectBox").prepend("<optionvalue='0'>Before Apples</option>");

6. Replaceall the options with new options

$("#selectBox").html("<optionvalue='1'>Some oranges</option><option value='2'>MoreOranges</option>");

7. Replaceitems at a certain index

$("#selectBoxoption:eq(1)").replaceWith("<option value='2'>Someapples</option>");

$("#selectBoxoption:eq(2)").replaceWith("<option value='3'>Somebananas</option>");

8. 지정된 index값으로 select 하기

$("#selectBoxoption:eq(2)").attr("selected", "selected");

9. text 값으로 select 하기

$("#selectBox").val("Someoranges").attr("selected", "selected");

10. value값으로 select 하기

$("#selectBox").val("2");

11. 지정된 인덱스값의 item 삭제

$("#selectBoxoption:eq(0)").remove();

12. 첫번째 item 삭제

$("#selectBoxoption:first").remove();

13. 마지막 item 삭제

$("#selectBoxoption:last").remove();

14. 선택된 옵션의 text 구하기

alert(!$("#selectBoxoption:selected").text());

15. 선택된 옵션의 value 구하기

alert(!$("#selectBoxoption:selected").val());

16. 선택된 옵션 index 구하기

alert(!$("#selectBoxoption").index($("#selectBox option:selected")));

17. SelecBox 아이템 갯수 구하기

alert(!$("#selectBoxoption").size());

18. 선택된 옵션 앞의 아이템 갯수

alert(!$("#selectBoxoption:selected").prevAl!l().size());

19. 선택된 옵션 후의 아이템 갯수

alert(!$("#selectBoxoption:selected").nextAll().size());

20. Insertan item in after a particular position

$("#selectBoxoption:eq(0)").after("<option value='4'>Somepears</option>");

21. Insertan item in before a particular position

$("#selectBoxoption:eq(3)").before("<option value='5'>Someapricots</option>");

22. Gettingvalues when item is selected

$("#selectBox").change(function(){

alert(!$(this).val());

alert(!$(this).children("option:selected").text());

});