Guest User

Untitled

a guest
Jul 20th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. CP = {};
  2.  
  3. Request.active = 0;
  4.  
  5. CP.State = new Class({
  6.  
  7. Implements: Options,
  8.  
  9. options: {
  10. countryField: null,
  11. stateField: null
  12. },
  13.  
  14. initialize : function(options) {
  15. this.setOptions(options);
  16. },
  17.  
  18. initSelect : function() {
  19.  
  20. var countryField = $(this.options.countryField);
  21.  
  22. if (countryField) {
  23. countryField.addEvent("change", function(event) {
  24. var countryId = event.target.options[event.target.selectedIndex].value;
  25. new Request.JSON({url : '/countries/' + countryId + '/states.json',
  26. onRequest: function() {
  27. Request.active++;
  28. },
  29. onSuccess: function(states) {
  30.  
  31. var selectOptions = new Element('select');
  32.  
  33. selectOptions.adopt(new Element('option', {
  34. 'value': '',
  35. 'html': 'Please select your county/state'
  36. }) )
  37.  
  38. for(var i = 0; i < states.length; i++) {
  39. state = states[i].state;
  40. selectOptions.adopt(new Element('option', {
  41. 'value': state.id,
  42. 'html': state.name
  43. }));
  44. }
  45.  
  46. $(this.options.stateField).set('html', selectOptions.innerHTML);
  47. $(this.options.stateField).set('disabled', (states.length < 1) ? true : false);
  48.  
  49. Request.active--;
  50.  
  51. }.bind(this) }).get();
  52. }.bind(this));
  53. }
  54.  
  55. }
  56.  
  57. });
  58.  
  59. window.addEvent('domready', function() {
  60.  
  61. var state = new CP.State({countryField: 'user_country_id', stateField: 'user_state_id'});
  62. state.initSelect();
  63.  
  64. });
Add Comment
Please, Sign In to add comment