±M·~º|¤ô³B²z¡A§K¶O²{³õ°É¬d¡Aºë·Ç¤uªk®Ú¥»¤îº|
»ù®æ¤½¹D¡A¤H­û²{³õ§K¶O¦ô»ù¡A¥ß§Y¨Ó¹q
ªoº£¤uµ{§ä«K©y¨Ó²³æ±þ»ùºô´N¹ï¤F¡AÅý±z»´ÃP¤ñ»ù¡A¬Ù®É¬Ù²ü¥]
µ´¹ï¤£¦YÁ«¡AÅýº¡¤Ñ³Û»ùªº¶Â¤ß¼t°ÓµL©Ò¹P§Î

­º­¶  ¡E  maids ½×¾Â ¡E µ{¦¡³]­p°Q½×     ¡E 

[JQuery] jQuery¹ïselectªº¾Þ�

©ÐªF¡Gªü³Í
µoªí®É¶¡¡G2011-04-10



  • get


    • ¨ú¥X¿ï¾Üªº­È

      $("select#Club").val();

      $('select#Club option:selected').text();


      ¥H¤W2¤èªk¦b³æ¿ï®É¬Û¦P¡A¦ý½Æ¿ï®É¡A

      val()·|¥Î³r¸¹¤À¶} ex. AA, BB

      text()¤£·| ex. AABB

    • ¨ú¥Xarray

      $("select#Club").children("[@selected]").each(function(){

          alert(this.text());

      });



  • set


    • ¨Ï¬YoptionÅܬ°selected

      $("#select1").children().each(function(){

          if ($(this).text()=="option you want"){

              //jQueryµ¹ªk

              $(this).attr("selected","true"); //©Î¬Oµ¹selected¤]¥i



              //javascriptµ¹ªk

              this.selected = true;

          }

      });

    • Åý·s¼Wªºoptionª½±µ¬°selected

      var option = jQuery("new option");

      $('
      select#Club').append(option);

      $(option).attr("selected","true"); //Åýoption¬°selected

      $('
      select#Club').trigger("change"); //³Ì«á­nIJµoselectªºchange¨Æ¥ó


    • select¤U©Ô®Øªº²Ä¤G­Ó¤¸¯À¬°·í«e¿ï¤¤­È

      $('select#Club')[0].selectedIndex = 1;//¤£ª¾¬°¦ó­n¥[[0]

      =========== K. T. Chen ´£¨ì ==========================

      ¦b$("")¥[[0]ªº·N«ä¬O§âjQueryª«¥óÂରDOMª«¥ó¡C³o¼Ë¤ljQueryª«¥ó¤~¯à¨Ï¥ÎDOM©³¤UªºselectedIndex¤èªk¡C



  • event

    //§ïÅܮɪº¨Æ¥ó

    $("select#Club").change(function(){ //¨Æ¥óµo¥Í

        //¤@¦¸¦L¥X

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



        //¦L¥X¿ï¨ì¦h­Ó­È

        jQuery('option:selected', this).each(function(){

            alert(this.value);

        });

    });

  • ²¾°£

    removeOption(index/value/regex[, selectedOnly])

    $('select#Clubs option:selected').remove();






//¯Âjavascirpt

<select onchange="alert('Index: ' + this.selectedIndex

+ '\nValue: ' + this.options[this.selectedIndex].value)">

...

</select>



°Ñ¦Ò






  • ÃÙ§Uºô¯¸       

    ¼s§Q¤£°Ê²£-·sªO¯S°Ï«ü¦W«×³Ì°ª¡B­È±o±z«H¿àªº¦n©Ð¥ò
    ±zªº¦«¥I,¼s§Q¥Î¤ß¬°±zªA°È
    ¼s§Q¤£°Ê²£-ªO¾ô¦b¦a¥Í®Ú³Ì¹ê¦b--·sªO¯S°Ï«ü¦W«×³Ì°ª¡B­È±o±z«H¿àªº¦n©Ð¥ò
    §¹¾ã©Ð°T¡A©Ð«Î¡B©±­±¼öªùºë¿ïª«¥ó¡A¼s§Q¤£°Ê²£ Àu½è¥ò¤¶¡A©Ð«Î¯²¸î¡B¶R½æ¸ê°T³z©ú¡A¥æ©ö¯u¦w¤ß¡I

  • 1 ¼Ó¦í¤á¡G¤p³Í
    µoªí®É¶¡¡G2011-04-12

    <!DOCTYPE html> 
    <html>
     
    <head>
     
     
    <style>
     
      img
    { padding:10px; }
     
      div
    { color:red; font-size:24px; }
     
    </style>
     
     
    <script src="http://code.jquery.com/jquery-1.5.js"></script>
     
    </head>
     
    <body>
     
       
     
    <img />
     
     
    <img />
     
     
    <img />
     
     
     
    <div><B>Attribute of Ajax</B></div>
     
     
    <script>
     
    $
    ("img").attr({
     
      src
    : "/images/hat.gif",
     
      title
    : "jQuery",
     
      alt
    : "jQuery Logo"
     
    });
     
    $
    ("div").text($("img").attr("alt"));
     
    </script>
     
     
    </body>
     
    </html>

    Demo:


     


    Example: Disable buttons greater than the 1st button.


    <!DOCTYPE html> 
    <html>
     
    <head>
     
     
    <style>
     
      button
    { margin:10px; }
     
    </style>
     
     
    <script src="http://code.jquery.com/jquery-1.5.js"></script>
     
    </head>
     
    <body>
     
       
     
    <button>0th Button</button>
     
     
    <button>1st Button</button>
     
     
    <button>2nd Button</button>
     
     
    <script>
     
    $
    ("button:gt(1)").attr("disabled","disabled");
     
    </script>
     
     
    </body>
     
    </html>

    Demo:


     



    Example: Set the id for divs based on the position in the page.


    <!DOCTYPE html> 
    <html>
     
    <head>
     
     
    <style>
     
      div
    { color:blue; }
     
      span
    { color:red; }
     
      b
    { font-weight:bolder; }
     
           
    </style>
     
     
    <script src="http://code.jquery.com/jquery-1.5.js"></script>
     
    </head>
     
    <body>
     
       
     
    <div>Zero-th <span></span></div>
     
     
    <div>First <span></span></div>
     
     
    <div>Second <span></span></div>
     
     
    <script>
     
    $
    ("div").attr("id", function (arr) {
     
     
    return "div-id" + arr;
     
    })
     
    .each(function () {
     
      $
    ("span", this).html("(ID = '<b>" + this.id + "</b>')");
     
    });
     
    </script>
     
     
    </body>
     
    </html>

    Demo:


     



    Example: Set the src attribute from title attribute on the image.


    <!DOCTYPE html> 
    <html>
     
    <head>
     
     
    <script src="http://code.jquery.com/jquery-1.5.js"></script>
     
    </head>
     
    <body>
     
       
    <img title="hat.gif"/>
     
     
    <script>
     
    $
    ("img").attr("src", function() {
     
       
    return "/images/" + this.title;
     
    });
     
    </script>
     
     
    </body>
     
    </html>

    Demo:


     





    ¡@¦@ 1 ¤H¦^À³¡@¡@¿ï¾Ü­¶¼Æ ¡i²Ä1 ­¶¡j 

    ©m¦W¡G
    §G§i¤º®e¡G