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. 付费素材资源,需充值后方能下载,如有任何疑问可直接联系苦力吧客服
相关资源 / 表单美化

js自定义选择下拉列表美化插件

一款下拉列表美化插件,一般都是使用select标签包裹实现下拉列表,在这里我们只需要使用ul li,然后结合css和javascript即可实现下拉选择效果。
  表单美化
 61045  0

jquery功能丰富的select下拉菜单插件

这是一款功能强大的select插件,兼容包括IE6在内的所有主流浏览器,提供了丰富的API和回调函数,超实用。
  表单美化
 3731  0

一款ios风格点击滑动radio单选表单美化按钮开关

点击按钮左右滑动动画,显示开关开启文字,很实用的单选按钮美化。
  表单美化
 1890  0

jquery带多种表单开关时钟按钮美化插件

一款支持自定义Neumorphic风格的UI工具包,基于CS3和Ionicons/Material/FontAwesome制作的表单美化UI图标。
  表单美化
 61171  0

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

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