SwVitaliy

Untitled

Aug 1st, 2012
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $.fn.subSelects = function(sels, vals) {
  2. function trueRefreshSelects(sels, vals) {var names = []; sels.each(function(){console.log(0);
  3. var $this = $(this);
  4. var prev = $this.prev();
  5. if (!$this.attr('name')) return undefined;
  6. if (!prev.hasClass('selectArea')) return undefined; console.log('1', $this.attr('name'));
  7. var m = prev.attr('id').match(/\d+$/); if (m.length === 0) return undefined; console.log('2', prev.attr('id'), m);
  8. var index = m[0];
  9. prev.remove();
  10. $this.find('option').remove();
  11. var b, d;
  12. do { d = document.getElementById("optionsDiv"+5); b = d != null; if (d) d.parentNode.removeChild(d); } while (b);console.log('3');
  13. var i, str = ''; for (i in vals) { if (!vals.hasOwnProperty(i)) continue; str+= '<option value="{index}">{value}</option>'.replace('{index}', i).replace('{value}', vals[i]) }
  14. $(str).appendTo($this);
  15. names.push($this.attr('name'));
  16. });
  17. refreshSelects(names);
  18. }
  19. function updateSelects(sels, vals) {
  20. var ctx = this.filter(':first');
  21. if (!ctx.is('select')) return undefined;
  22. var valKey = ctx.val();
  23. var vals = vals[valKey];
  24. if (typeof vals == 'undefined') throw 'unknown values for key {valKey}'.replace('{valKey}', valKey);
  25. trueRefreshSelects(sels, vals);
  26. }
  27. updateSelects.apply($(this), [$(sels), vals]);
  28. }
Advertisement
Add Comment
Please, Sign In to add comment