限时优惠活动
亲爱的苦力吧用户,我们为了回馈新老用户一直以来的鼎力支持,即日起(2025-02-06至2025-03-06)凡是购买苦力吧VIP/充值K币的新老用户,都直接可获得买一送一的优惠馈赠。例如:购买一个月的VIP会员可直接获得两个月的VIP会员;充值100K币可直接获得200K币,以此类推!有任何疑问可联系在线客服,感谢各位用户对苦力吧素材的信任与厚爱,我们将一如既往的给大家上新更多优质的素材源码,祝大家开工大吉、工作顺利、心想事成。

jquery从json文件动态获取数据更新select下拉菜单插件

所属分类: 网页特效-表单美化    2023-10-25 09:22:12

jquery从json文件动态获取数据更新select下拉菜单插件 ie兼容6
 查看演示  登录后下载 温馨提示
登录会员即可享受免费下载
 我要建站

jquery从json文件动态获取数据更新select下拉菜单插件(共3个文件)

    • json.json
    • musiciansddropdown.js
    • index.html

使用方法

(function($) {

    $.fn.dynamicDropdown = function(options) {

        // Defining default settings for the plugin.
        var settings = $.extend({
            url: '',
            key: '',
            value: '',
        }, options);

        // if the target is not a select, exits.
        if ( $(this)[0].tagName != 'SELECT' )
        {
          console.log('ERROR: Target is not a select object');
          return false;
        }

        // if the target doesn't have the class form-control from bootstrap, then is added to the target.
        if ( $(this)[0].className != 'form-control' ) {
          $(this).addClass('form-control');
        }

        // Create variable to reference 'this' object and use it later.
        let this_obj = $(this);

        // If the url was not defined or is empty, it raises an error and exits the execution.
        if (!settings.url || settings.url == '') {
          console.log('%c ERROR: Please provide an URL in the settings of the dropdown', 'background: red');
          return false;
        }

        // AJAX call to the url provided in the plugin's settings.
        $.getJSON(settings.url, function(result) {


          if (settings.key != '' && settings.value == '') {

              /* creates key object with the name of the parameter in the AJAX function above
                 concatenated with the value in settings.key. */
              var key_obj = 'result.' + settings.key;

              // for each key in result.{name of the key}...
              Object.keys(eval(key_obj)).forEach(function(x){

                // checks if the key is not an object, if true, it skips the object.
                if(typeof eval(key_obj)[x] !== 'object')
                {
                  this_obj.append('');
                }
              });

          // If settings.key is empty and settings.value is not empty...
          } else if (settings.key == '' && settings.value != '') {

              // For each key in result...
              Object.keys(result).forEach(function(x){

                // append the keys at settings.value to this_obj, i.e the select object.
                this_obj.append('');
              });

          } else {
              var key_obj = 'result.' + settings.key;
              Object.keys(eval(key_obj)).forEach(function(x){
                this_obj.append('');
              });
          }


        // If the call to the URL don't return a json object, it returns an error.
        }).fail(function() {
            console.log('%c ERROR: ' + 'Response data from ' + settings.url + ' is not a JSON object',
                'background: red');
        })
    };

}(jQuery));

站长提示:
1. 苦力吧素材官方QQ群:950875342
2. 平台上所有素材资源,需注册登录会员方能正常下载。
3. 会员用户积极反馈网站、素材资源BUG或错误问题,每次奖励2K币
4. PHP源码类素材,如需协助安装调试,或你有二次开发需求,可联系苦力吧客服。
5. 付费素材资源,需充值后方能下载,如有任何疑问可直接联系苦力吧客服
相关资源 / 表单美化

CSS实现的可拉伸弹性效果切换按钮

一款鼠标点击按钮触发拉伸动画特效,点击按钮可切换开关状态。
  表单美化
 3920  0

jquery下拉框select表单美化插件

一款蓝色风格下拉菜单美化代码,代码简单实用。
  表单美化
 5324  0

jquery支持搜索过滤的下拉菜单美化插件

一款轻量级自定义select下拉菜单组件,具备模糊搜索和过滤特效,还可以全选。
  表单美化
 3370  0

jquery超全的格式化数字输入美化插件

超多种input不同数据类型表单美化,可根据需求自定义属于你的数据输入。
  表单美化
 1318  0

评论数(0) 回复有机会获得K币 用户协议

^_^ 还没有人评论,快来抢个沙发!
😀
  • 😀
  • 😊
  • 😂
  • 😍
  • 😑
  • 😷
  • 😵
  • 😛
  • 😣
  • 😱
  • 😋
  • 😎
  • 😵
  • 😕
  • 😶
  • 😚
  • 😜
  • 😭
发表评论