简体   繁体   English

在数组的方法调用中设置选项

[英]Setting options in a method call with an array

How can I make it so the .slider() method uses the values from the array? 我怎样才能使.slider()方法使用数组中的值?

I have this same slider I have to set up several times, so I figured I would call it once, and input the values with the index. 我有相同的滑块,我必须设置几次,所以我想一次可以调用它,然后输入带有索引的值。 This might not be the way to do it but it's what came to me. 这可能不是这样做的方法,但这就是我想到的。

I wanted to step through and change the index number each time I call the slider so it would update to the next 6 values. 我想逐步浏览并在每次调用滑块时更改索引号,以便它将更新为下一个6个值。 I realize this code doesn't work, it's just for explaining. 我意识到这段代码行不通,仅用于解释。

var yolo = ["yield", "true", "15.2", "308", "0.3", "[75.8, 241.4]", "tensile", "true", "37.7", "345", "[81.2, 243.3]"];

     $( yolo[0] ).slider({
        range: yolo[1],
        min: yolo[2],
        max: yolo[3],
        step : yolo[4],
        values: yolo[5],

        slide: function( event, ui ) {
            $( "#" + yolo[0] + "-value" ).val( ui.values[ 0 ] + " - " + ui.values[ 1 ] );
        }
     }); //When done, call again, just that updating the index numbers to the next 6

Maybe an .each() with $.extend() ? 也许一个.each()$.extend()

You can do something as: 您可以执行以下操作:

var settings = {
    "yield" : {
         range: true,
         etc..
    },
    "yield2" : {
         range: true,
         etc..
    }
}

$.each(settings, function(key, value){
    var _key = key;
    $( "#" + _key ).slider(
        $.extend(value, {    
            slide: function( event, ui ) {
                $( "#" + _key + "-value" ).val( ui.values[ 0 ] + " - " + ui.values[ 1 ] );
            }
        })
    );
});

Extra info: 额外信息:

  1. jQuery.extend jQuery.extend
  2. jQuery.each jQuery.each

使用.each http://api.jquery.com/each/遍历数组。

Instead of using an array, you can just use a plain object: 除了使用数组,您还可以使用普通对象:

var yolo = {
   range : true,
   min : 15.2,
   max : 308,
   slide : function(){}
};

and then pass that into slider. 然后将其传递到滑块。

$(...).slider(yolo);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM