Guest User

Kod again.

a guest
Aug 2nd, 2012
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.86 KB | None | 0 0
  1. *test.php*
  2.  
  3. <html>
  4.  
  5. <head>
  6.  
  7. <script src="jquery.chained.js" type="text/javascript" charset="utf-8"></script>
  8. <script src="jquery_states.js" type="text/javascript" charset="utf-8"></script>
  9. <script src="jquery.js" type="text/javascript" charset="utf-8"></script>
  10.  
  11. </head>
  12.  
  13. <body>
  14.  
  15.  
  16. <select id="mark">
  17.   <option value="">--</option>
  18.   <option value="bmw">BMW</option>
  19.   <option value="audi">Audi</option>
  20. </select>
  21. <select id="series">
  22.   <option value="">--</option>
  23.   <option value="series-3" class="bmw">3 series</option>
  24.   <option value="series-5" class="bmw">5 series</option>
  25.   <option value="series-6" class="bmw">6 series</option>
  26.   <option value="a3" class="audi">A3</option>
  27.   <option value="a4" class="audi">A4</option>
  28.   <option value="a5" class="audi">A5</option>
  29. </select>
  30.  
  31.  
  32. </body>
  33.  
  34.  
  35.  
  36. </html>
  37.  
  38. /* jquery.chained.js  */
  39.  
  40. /*
  41.  * Chained - jQuery non AJAX(J) chained selects plugin
  42.  *
  43.  * Copyright (c) 2010-2011 Mika Tuupola
  44.  *
  45.  * Licensed under the MIT license:
  46.  *   http://www.opensource.org/licenses/mit-license.php
  47.  *
  48.  */
  49.  
  50. (function($) {
  51.  
  52.     $.fn.chained = function(parent_selector, options) {
  53.        
  54.         return this.each(function() {
  55.            
  56.             /* Save this to self because this changes when scope changes. */            
  57.             var self   = this;
  58.             var backup = $(self).clone();
  59.                        
  60.             /* Handles maximum two parents now. */
  61.             $(parent_selector).each(function() {
  62.                                                
  63.                 $(this).bind("change", function() {
  64.                     $(self).html(backup.html());
  65.  
  66.                     /* If multiple parents build classname like foo\bar. */
  67.                     var selected = "";
  68.                     $(parent_selector).each(function() {
  69.                         selected += "\\" + $(":selected", this).val();
  70.                     });
  71.                     selected = selected.substr(1);
  72.  
  73.                     /* Also check for first parent without subclassing. */
  74.                     /* TODO: This should be dynamic and check for each parent */
  75.                     /*       without subclassing. */
  76.                     var first = $(parent_selector).first();
  77.                     var selected_first = $(":selected", first).val();
  78.                
  79.                     $("option", self).each(function() {
  80.                         /* Remove unneeded items but save the default value. */
  81.                         if (!$(this).hasClass(selected) &&
  82.                             !$(this).hasClass(selected_first) && $(this).val() !== "") {
  83.                                 $(this).remove();
  84.                         }                        
  85.                     });
  86.                
  87.                     /* If we have only the default value disable select. */
  88.                     if (1 == $("option", self).size() && $(self).val() === "") {
  89.                         $(self).attr("disabled", "disabled");
  90.                     } else {
  91.                         $(self).removeAttr("disabled");
  92.                     }
  93.                     $(self).trigger("change");
  94.                 });
  95.                
  96.                 /* Force IE to see something selected on first page load, */
  97.                 /* unless something is already selected */
  98.                 if ( !$("option:selected", this).length ) {
  99.                     $("option", this).first().attr("selected", "selected");
  100.                 }
  101.        
  102.                 /* Force updating the children. */
  103.                 $(this).trigger("change");            
  104.  
  105.             });
  106.         });
  107.     };
  108.    
  109.     /* Alias for those who like to use more English like syntax. */
  110.     $.fn.chainedTo = $.fn.chained;
  111.    
  112. })(jQuery);
  113.  
  114.  
  115. /* jquery.js */
  116.  
  117. Fil klar
  118.  
  119.  
  120. /* jquery_states.js */
  121.  
  122. $(function(){
  123.      $("#series").chained("#mark"); /* or $("#series").chainedTo("#mark"); */
  124. });
Advertisement
Add Comment
Please, Sign In to add comment