jquery下拉框动态绑值_jQuery基于URL的动态组合框值

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下拉框动态绑值

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值