{"id":3165,"date":"2014-01-09T15:22:47","date_gmt":"2014-01-09T06:22:47","guid":{"rendered":"http:\/\/auctionpro.co.kr\/?p=3165"},"modified":"2019-06-05T17:29:11","modified_gmt":"2019-06-05T08:29:11","slug":"jquery-%ec%bd%a4%eb%b3%b4%eb%b0%95%ec%8a%a4-select-%ec%a0%9c%ec%96%b4","status":"publish","type":"post","link":"https:\/\/www.auctionpro.co.kr\/?p=3165","title":{"rendered":"jQuery \ucf64\ubcf4\ubc15\uc2a4  select \uc81c\uc5b4"},"content":{"rendered":"\n<pre class=\"lang:js decode:true \" >jQuery\ub85c \uc120\ud0dd\ub41c \uac12 \uc77d\uae30\r\n$(\"#selectBox option:selected\").val();\r\n$(\"select[name=name]\").val();\r\n \r\njQuery\ub85c \uc120\ud0dd\ub41c \ub0b4\uc6a9 \uc77d\uae30\r\n$(\"#selectBox option:selected\").text();\r\n \r\n\uc120\ud0dd\ub41c \uc704\uce58\r\nvar index = $(\"#test option\").index($(\"#test option:selected\"));\r\n \r\n-------------------------------------------------------------------\r\n \r\n\/\/ Add options to the end of a select\r\n$(\"#selectBox\").append(\"&lt;option value='1'&gt;Apples&lt;\/option&gt;\");\r\n$(\"#selectBox\").append(\"&lt;option value='2'&gt;After Apples&lt;\/option&gt;\");\r\n \r\n\/\/ Add options to the start of a select\r\n$(\"#selectBox\").prepend(\"&lt;option value='0'&gt;Before Apples&lt;\/option&gt;\");\r\n \r\n\/\/ Replace all the options with new options\r\n$(\"#selectBox\").html(\"&lt;option value='1'&gt;Some oranges&lt;\/option&gt;&lt;option value='2'&gt;More Oranges&lt;\/option&gt;\");\r\n \r\n\/\/ Replace items at a certain index\r\n$(\"#selectBox option:eq(1)\").replaceWith(\"&lt;option value='2'&gt;Some apples&lt;\/option&gt;\");\r\n$(\"#selectBox option:eq(2)\").replaceWith(\"&lt;option value='3'&gt;Some bananas&lt;\/option&gt;\");\r\n \r\n\/\/ \uc9c0\uc815\ub41c index \uac12\uc73c\ub85c select \ud558\uae30\r\n$(\"#selectBox option:eq(2)\").attr(\"selected\", \"selected\");\r\n \r\n\/\/ text \uac12\uc73c\ub85c select \ud558\uae30\r\n$(\"#selectBox\").val(\"Some oranges\").attr(\"selected\", \"selected\");\r\n \r\n\/\/ value \uac12\uc73c\ub85c select \ud558\uae30\r\n$(\"#selectBox\").val(\"2\");\r\n \r\n\/\/ \uc9c0\uc815\ub41c \uc778\ub371\uc2a4 \uac12\uc758 item \uc0ad\uc81c\r\n$(\"#selectBox option:eq(0)\").remove();\r\n \r\n\/\/ \uccab\ubc88\uc9f8 item \uc0ad\uc81c\r\n$(\"#selectBox option:first\").remove();\r\n \r\n\/\/ \ub9c8\uc9c0\ub9c9 item \uc0ad\uc81c\r\n$(\"#selectBox option:last\").remove();\r\n \r\n\/\/ \uc120\ud0dd\ub41c \uc635\uc158\uc758 text \uad6c\ud558\uae30\r\nalert($(\"#selectBox option:selected\").text());\r\n \r\n\/\/ \uc120\ud0dd\ub41c \uc635\uc158\uc758 value \uad6c\ud558\uae30\r\nalert($(\"#selectBox option:selected\").val());\r\n \r\n\/\/ \uc120\ud0dd\ub41c \uc635\uc158 index \uad6c\ud558\uae30\r\nalert($(\"#selectBox option\").index($(\"#selectBox option:selected\")));\r\n \r\n\/\/ SelecBox \uc544\uc774\ud15c \uac2f\uc218 \uad6c\ud558\uae30\r\nalert($(\"#selectBox option\").size());\r\n \r\n\/\/ \uc120\ud0dd\ub41c \uc635\uc158 \uc55e\uc758 \uc544\uc774\ud15c \uac2f\uc218\r\nalert($(\"#selectBox option:selected\").prevAll().size());\r\n \r\n\/\/ \uc120\ud0dd\ub41c \uc635\uc158 \ud6c4\uc758 \uc544\uc774\ud15c \uac2f\uc218\r\nalert($(\"#selectBox option:selected\").nextAll().size());\r\n \r\n\/\/ Insert an item in after a particular position\r\n$(\"#selectBox option:eq(0)\").after(\"&lt;option value='4'&gt;Some pears&lt;\/option&gt;\");\r\n \r\n\/\/ Insert an item in before a particular position\r\n$(\"#selectBox option:eq(3)\").before(\"&lt;option value='5'&gt;Some apricots&lt;\/option&gt;\");\r\n \r\n\/\/ Getting values when item is selected\r\n$(\"#selectBox\").change(function() {\r\n           alert($(this).val());\r\n           alert($(this).children(\"option:selected\").text());\r\n});\r\n[\ucd9c\ucc98] jQuery \ucf64\ubcf4 select \uc81c\uc5b4\ud558\uae30|\uc791\uc131\uc790 \uc5f4\ub9b0\ubb38\uc11c\r\n \uc0ad\uc81c\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>jQuery\ub85c \uc120\ud0dd\ub41c \uac12 \uc77d\uae30 $(&#8220;#selectBox option:selected&#8221;).val(); $(&#8220;select[name=name]&#8221;).val(); jQuery\ub85c \uc120\ud0dd\ub41c \ub0b4\uc6a9 \uc77d\uae30 $(&#8220;#selectBox option:selected&#8221;).text(); \uc120\ud0dd\ub41c \uc704\uce58 var index = $(&#8220;#test option&#8221;).index($(&#8220;#test option:selected&#8221;)); &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- \/\/ Add options <a class=\"mh-excerpt-more\" href=\"https:\/\/www.auctionpro.co.kr\/?p=3165\" title=\"jQuery \ucf64\ubcf4\ubc15\uc2a4  select \uc81c\uc5b4\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[8],"tags":[],"class_list":["post-3165","post","type-post","status-publish","format-standard","hentry","category-jquery"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.auctionpro.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/3165","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.auctionpro.co.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.auctionpro.co.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.auctionpro.co.kr\/index.php?rest_route=\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.auctionpro.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3165"}],"version-history":[{"count":0,"href":"https:\/\/www.auctionpro.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/3165\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.auctionpro.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.auctionpro.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.auctionpro.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}