jquery下拉框动态绑值
jQuery的小功能可根据URL字符串中给出的参数动态设置组合框的值。 对于基于用户为搜索条件选择的内容在表单结果页面上设置默认值可能很有用。
当您在网址中未指定参数时(即非“ param = 1&param = 2”),但是当该网址对于SQL查询可能具有一个巨大的参数(例如“ select = fields + from + table + like +”)时,此函数起作用combovaluevalue + etc”。 您可以指定要查找的值之前的字符串(即发现我们将获得值combovaluevalue)。
/* This function sets the combobox with the value after "like" inside the url */
(function($) {
//get the url variables and set the combo box
var comboBox = $(location).attr('href');
comboBox = decodeURIComponent(comboBox); //decode url string
comboBox = comboBox.replace(/"/g, ''); //replace quotes
var urlArray = comboBox.split("+"); //get params
//the param we're looking for is after "like"
comboBox = urlArray[jQuery.inArray("like", urlArray)+1];
$('#combobox-id > option').each(function(index) {
//alert($(this).text() + ' ' + $(this).val());
console.log(index + " " + $(this).attr('value'));
if ($(this).attr('value') === comboBox) {
comboBox = index;
}
});
$('#combobox-id').attr('selectedIndex', jQuery.inArray(comboBox, urlArray));
})(jQuery);
翻译自: https://www.sitepoint.com/dynamically-combobox-value-url/
jquery下拉框动态绑值