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

js自定义鼠标跟随动画特效代码

这是一款鼠标跟随动画,自定义的光标自动跟踪鼠标移动,增加了网站的用户体验和交互性。其实就是使用了自定义光标替换了默认光标,再实时获取光标xy坐标位置。
  表单美化
 167  

jquery带IOS风格的开关切换按钮特效代码

一款checkbox美化插件,将复选框转换为iOS风格的切换按钮,保留了标签代码check和uncheck函数。
  表单美化
 190  

jquery支持单选多选的DIV元素插件

一款DIV单选多选插件,可跟checkbox和radio混合使用,很有创新的特效。
  表单美化
 204  

jquery页面上滑滚动拖动手型插件

一款页面滚动拖动插件,用户可通过鼠标拖动滚动页面,类似于移动端的上下滑动行为。
  表单美化
 205  

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

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