2010年3月24日

[jQuery] Quick Note

This is only a note for my first jQuery html page. The following codes allow user to move focus between textbox on the page via arrow keys(Up/Down)

   1: <script type="text/javascript">



   2:     $(function () {



   3:         var total = 0;



   4:         $("input").each(function (i) {



   5:             if ($(this).attr("type") == "text") {



   6:                 $(this).addClass("tabIndexedInput").attr("idx", total++);



   7:             }



   8:         });



   9:         $("input.tabIndexedInput").live("keydown", function (evt) {



  10:             var idx = parseInt($(this).attr("idx"));



  11:             switch (evt.which) {



  12:                 case 38:



  13:                     idx -= 1;



  14:                     break;



  15:                 case 40:



  16:                     idx += 1;



  17:                     break;



  18:             }



  19:             if (idx >= 0) {



  20:                 $("input.tabIndexedInput[idx=" + idx + "]").focus();



  21:                 return false;



  22:             } else {



  23:                 return true;



  24:             }



  25:         });



  26:     });



  27: </script>


沒有留言:

About Me