Created
January 17, 2015 15:18
-
-
Save Ulv/b5372b43279e60b3b8e6 to your computer and use it in GitHub Desktop.
JavaScript GET parameter to object - fill form field
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* автоматом отмечает значения, которые пришли в переменных GET | |
* запрос - строка вида worktype=100&second=35 | |
* | |
* Здесь: | |
* worktype - имя и значение option основного селекта | |
* second - имя и значение option зависимого (второго) селекта | |
*/ | |
$.fn.extend({ | |
diplomGetselect: function() { | |
/* разбор GET параметров */ | |
var getParams, urldecode; | |
getParams = function() { | |
var key, params, query, raw_vars, v, val, _i, _len, _ref; | |
query = window.location.search.substring(1); | |
raw_vars = query.split("&"); | |
params = {}; | |
for (_i = 0, _len = raw_vars.length; _i < _len; _i++) { | |
v = raw_vars[_i]; | |
_ref = v.split("="), key = _ref[0], val = _ref[1]; | |
params[key] = decodeURIComponent(val); | |
} | |
return params; | |
}; | |
urldecode = function(url) { | |
return decodeURIComponent(url.replace(/\+/g, ' ')); | |
}; | |
return this.each(function() { | |
var $select, i, p, params, _results; | |
params = getParams(); | |
// input / hidden etc | |
if (typeof params.ovuz != 'undefined' && params.ovuz.length) { | |
$('#VUZ').val(urldecode(params.ovuz)); | |
} | |
// select | |
if (typeof params.otype != 'undefined' && params.otype.length) { | |
$('#second').find("option[value='" + urldecode(params.otype) + "']").attr("selected", true).trigger("refresh"); | |
} | |
return _results; | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment