code_junkie

Readonly SELECT tag

Nov 12th, 2011
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. <select id="countries" onfocus="this.defaultIndex=this.selectedIndex;" onchange="this.selectedIndex=this.defaultIndex;">
  2. <option value="1">Country1</option>
  3. <option value="2">Country2</option>
  4. <option value="3">Country3</option>
  5. <option value="4">Country4</option>
  6. <option value="5">Country5</option>
  7. <option value="6">Country6</option>
  8. <option value="7" selected="selected">Country7</option>
  9. <option value="8">Country8</option>
  10. <option value="9">Country9</option>
  11. </select>
  12.  
  13. var readonlySelect = function(selector, makeReadonly) {
  14.  
  15. $(selector).filter("select").each(function(i){
  16. var select = $(this);
  17.  
  18. //remove any existing readonly handler
  19. if(this.readonlyFn) select.unbind("change", this.readonlyFn);
  20. if(this.readonlyIndex) this.readonlyIndex = null;
  21.  
  22. if(makeReadonly) {
  23. this.readonlyIndex = this.selectedIndex;
  24. this.readonlyFn = function(){
  25. this.selectedIndex = this.readonlyIndex;
  26. };
  27. select.bind("change", this.readonlyFn);
  28. }
  29. });
  30.  
  31. };
  32.  
  33. $("select.myselect").bind("focus", function(){
  34. if($(this).hasClass('readonly'))
  35. {
  36. $(this).blur();
  37. return;
  38. }
  39. });
  40.  
  41. $('select[name=country]').attr("disabled", "disabled");
Add Comment
Please, Sign In to add comment