【问题标题】:Is possible use 'div id' as name of array?是否可以使用“div id”作为数组的名称?
【发布时间】:2014-06-02 21:20:42
【问题描述】:

请查看此 jsfiddle jsfiddle.net/rflfn/uS4jd/

这是另一个尝试jsfiddle.net/rflfn/T3ZT6/

我正在使用SMOF 开发Wordpress 主题,我需要在单击链接时创建一个函数来更改某些值,但是当我使用div 名称创建数组时,该数组返回空值...

HTML:

<a class="button" id="settext1">Some Link</a>
<br />

<a class="button" id="settext2">Another Link</a>
<br />

<a class="button" id="settext3">Link 3</a>
<br />

jQuery:

$(document).ready(function(){
    // var col_settext1 = new Array(); // <-- I need make this array with name of DIV cliked
    col_settext1['field_id1']='#FF0000';
    col_settext1['field_id2']='#00FFFF';

    // var txt_settext1 = new Array(); // <-- I need make this array with name of DIV cliked
    txt_settext1['field_id3']='Some Text Here';
    txt_settext1['field_id4']='Another Text Here';

    // var txt_settext2 = new Array(); // <-- I need make this array with name of DIV cliked
    txt_settext2['field_id5']='Some Text Here';

    // var col_settext2 = new Array(); // <-- I need make this array with name of DIV cliked
    col_settext2['field_id6']='Another Text Here';

    // var chk_settext2 = new Array(); // <-- I need make this array with name of DIV cliked
    chk_settext2['field_id7']="checked";
});

$('.button').click(function(){
    $myclass = this.id;

    $col = 'col_' + $myclass;
    $txt = 'txt_' + $myclass;
    $chk = 'chk_' + $myclass;

    // Based I clicked on the link 'settext1', Here I have this:
    // col_settext1
    // txt_settext1
    // chk_settext1

    // THE PROBLEM ARE HERE!
    $col = new Array(); // <--- Here I use name of DIV as Array, but the value is lost...
    $txt = new Array();
    $chk = new Array();

    // Test...
    alert($col); // <--- Here no have any value :(
    alert($col[1]); // <--- Here no have any value :(

    for (id in $col) { // 'id' is value of array --> col_settext1['field_id1']='#FF0000';
        // do function based on array values...
        // just example:
        alert(id);
    }
    for (id in $txt) { // 'id' is value of array --> txt_settext1['field_id1']='#FF0000';
        // do function based on array values...
    }
    for (id in $chk) { // 'id' is value of array --> chk_settext1['field_id1']='#FF0000';
        // do function based on array values...
    }

});

是否可以使用 div 的名称作为数组名称? 欢迎提出任何解决此问题的建议或任何其他方法。

【问题讨论】:

    标签: jquery css arrays wordpress frameworks


    【解决方案1】:

    如果我正确理解您的问题(即您想根据某些参数创建动态变量名称),您应该使用下面的代码而不是“$col = new Array();”

    window[$col] = new Array();
    window[$txt] = new Array();
    

    等等。

    【讨论】:

    • 我需要使用具有相同名称的 DIV id 的数组,该数组已经存在并且数组的值也已经存在,我需要做的只是单击带有 ID 的链接并将数组与ID 同名,但不起作用...请查看此代码以更好地理解jsfiddle.net/rflfn/sZJ37/,在出现消息“值返回空白...”的窗口中,我需要显示数组中的值,但不返回值:(
    【解决方案2】:

    如果我正确解释了您的要求,您的问题是您实际上并没有将变量名称设置为新数组。

    现阶段:

    $myclass = this.id;
    
    $col = 'col_' + $myclass;
    $txt = 'txt_' + $myclass;
    $chk = 'chk_' + $myclass;
    

    你会有 $col = "col_id", $txt = "txt_id", $chk = "chk_id"。

    基本上,这三个变量中的每一个都绑定到一个字符串。

    现在你可以这样做了:

    $col = new Array();
    $txt = new Array();
    $chk = new Array();
    

    所以现在这三个变量中的每一个都绑定到一个全新的数组,其中没有任何条目。

    你想做的是这样的:

    $col = new Array();
    $col[0] = "col_" + $myclass;
    

    其他人也一样。这将创建一个新数组,其中包含一个条目“col_id”,绑定到变量名 $col。

    这将为您提供名为 $col、$txt 和 $chk 的数组,其中包含 id 名称。

    如果您想实际创建动态变量名称(不确定您是否真的需要这个,但可以肯定),您需要使用Window

    【讨论】:

    • 差不多这样,但是这样一来数组上的值就丢失了,我需要保留它在数组中的值,根据被点击的div的名字调用数组,为了更好地理解你可以看看这段代码jsfiddle.net/rflfn/sZJ37,注意最后一个窗口没有返回数组中的值,这就是问题所在。
    • 在这种情况下,您要么只想将字符串附加到数组中已有的内容,要么使用 Window 技术(如果您熟悉 Python 中的 Globals()那)使用 div ID 的正确名称创建新数组,然后将数据复制到其中。
    • 是的,这正是我的想法,但我有超过 200 个不同值的数组​​,所以我只想使用一个函数,而不是用几乎相同的代码做 200 个函数。
    • 我不确定您属于这两种情况中的哪一种,但无论哪种方式,我认为最好编写一个函数,该函数接受一个数组和一个名称并将两者绑定。你不应该做同样的事情 200 次。
    【解决方案3】:

    看起来很简单,我对 jquery 的了解很少,我整整两天都在尝试完成这项工作,但一无所获。谁知道这件事是五分钟。我创建了另一个代码,我需要的只是让警报返回所需的值,然后我可以让我的原始代码工作。

    通过单击按钮 1,我应该会看到警报“文本 1”,但没有任何反应。

    http://jsfiddle.net/rflfn/N4a2h/

    html

    <a class="button" id="arr1">Button1</a>
    <a class="button" id="arr2">Button2</a>
    <a class="button" id="arr3">Button3</a>
    

    jQuery

    $('.button').click(function () {
    
        var window[myvar] = this.id;
        window[myvar] = new Array(); // I need use ID as array name
    
        // Code for button1
        arr1['value1'] = 'text 1';
        arr1['value2'] = 'text 2';
    
        // Code for button2
        arr2['value3'] = 'text 3';
        arr2['value4'] = 'text 4';
    
        // Code for button3
        arr2['value5'] = 'text 5';
        arr2['value6'] = 'text 6';
    
        // Click on Button1, the alert below need show "text 1"
        alert(window[myvar['value1']]); // "text 1"
    
        // alert(myvar['value1']);
    });
    

    我发现了这个 http://www.sitepoi...post5174213 但我也不能让它工作。

    我尝试使用另一个Dynamic jQuery variable names,但我也无法让它工作。

    【讨论】:

      【解决方案4】:

      我在 jQuery 论坛上询问,终于得到了解决方案 https://forum.jquery.com/topic/is-possible-use-div-id-as-name-of-array-that-already-have-value

      http://jsfiddle.net/L4CDd/

      HTML

      <a class="button" data-arr="arr1">Button1</a> |
      <a class="button" data-arr="arr2">Button2</a> |
      <a class="button" data-arr="arr3">Button3</a>
      

      jQuery

      var arr = {
          arr1: {value1:"test 1", value2:"test 2"},
          arr2:{value3:"test 3", value4:"test 4"},
          arr3:{value5:"test 5", value6:"test 6"}
      }
      $('.button').click(function () {
          var call_array = arr[$(this).data("arr")]
          $.each(call_array, function (index, value) {
              alert('Array Value: ' + index + '        Array Content: ' + value);
          });
      });
      

      感谢您的帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-03-24
        • 1970-01-01
        • 2013-09-19
        • 1970-01-01
        • 1970-01-01
        • 2022-10-25
        • 1970-01-01
        相关资源
        最近更新 更多