jQuery从数据属性中选择数组

时间:2017-01-11 09:11:11

标签: arrays string html5 parameters nouislider

我正在尝试为makeSlider函数提供一个数组。 HTML5 Data属性选择正确的数组。不幸的是,它不被解释为数组。

不起作用:

 values = $(this).data("slider");
 makeSlider(sliderID, targetID, values);

正在工作:

makeSlider(sliderID, targetID, dataMehrfachauswahlmatrixLeft);

HTML:

    <div class="noui-slider" id="slider-1" data-slider="dataMehrfachauswahlmatrixLeft">
      <!-- slider here -->
    </div>
    <div class="hidden-md-up">
      <!-- Store selected value -->
      <input class="form-control slider-value" id="input-target-1">
    </div>

JS(script.js):

$(document).ready(function() {

  // slider array

    // Matrix 1
    var dataMehrfachauswahlmatrixLeft = [{
      id: 1,
      value: "sehr wichtig"
    }, {
      id: 2,
      value: ""
    }, {
      id: 3,
      value: ""
    }, {
      id: 4,
      value: ""
    }, {
      id: 5,
      value: "unwichtig"
    }, {
      id: null,
      value: "weiß nicht"
    }];

    // Matrix 2
    var dataMehrfachauswahlmatrixRight = [{
      id: 1,
      value: "in hohem Maße"
    }, {
      id: 2,
      value: ""
    }, {
      id: 3,
      value: ""
    }, {
      id: 4,
      value: ""
    }, {
      id: 5,
      value: "in geringem Maße"
    }, {
      id: null,
      value: "weiß nicht"
    }];

  // build slider

  var makeSlider = function(sliderID, targetID, values) {
    ...
  }

  // init slider

  $(".noui-slider").each(function(index) {
    var sliderID = $(this).attr("id"),
        targetID = $(this).next().find(".slider-value").attr("id");
        values = $(this).data("slider");

    makeSlider(sliderID, targetID, values);
  });
});

1 个答案:

答案 0 :(得分:1)

当前实现的问题是变量Sub NeedleSearch() Dim SearchSpace As Variant Dim found As Boolean found = False SearchSpace = Range("A1:z20").Value Dim i As Integer, j As Integer For i = 1 To 20 For j = 1 To 26 If SearchSpace(i, j) = "needle" Then Range("A25").Value = "Column " & j Range("B25").Value = "Row " & i found = True End If Next j Next i If found = False Then Range("A25").Value = "needle not found" Range("B25").Value = " " End If End Sub 没有引用定义的数组。它是一个字符串文字,其值为 dataMehrfachauswahlmatrixLeft dataMehrfachauswahlmatrixRight

value或任何其他对象范围中定义数组。

window

然后使用Bracket notation访问对象动态属性。

// Matrix 1
window.dataMehrfachauswahlmatrixLeft = [...];

// Matrix 2
window.dataMehrfachauswahlmatrixRight = [...]

这里的一个例子

&#13;
&#13;
values = $(this).data("slider");
makeSlider(sliderID, targetID, window[values]);
&#13;
$(document).ready(function() {
    var myOBj = {};
    // slider array
    // Matrix 1
    myOBj.dataMehrfachauswahlmatrixLeft = [{
        id: 1,
        value: "sehr wichtig"
    }, {
        id: 2,
        value: ""
    }, {
        id: 3,
        value: ""
    }, {
        id: 4,
        value: ""
    }, {
        id: 5,
        value: "unwichtig"
    }, {
        id: null,
        value: "weiß nicht"
    }];
    // Matrix 2
    myOBj.dataMehrfachauswahlmatrixRight = [{
        id: 1,
        value: "in hohem Maße"
    }, {
        id: 2,
        value: ""
    }, {
        id: 3,
        value: ""
    }, {
        id: 4,
        value: ""
    }, {
        id: 5,
        value: "in geringem Maße"
    }, {
        id: null,
        value: "weiß nicht"
    }];
    // build slider
    var makeSlider = function(sliderID, targetID, values) {
        console.log(values)
    }
    $(".noui-slider").on('click', function() {
        var values = $(this).data("slider");
        makeSlider(1, 2, myOBj[values]);
    });
});
&#13;
&#13;
&#13;