Jquery 是否可以使用';部门id';作为数组的名称?
请查看此JSFIDLE 这是另一种尝试 我正在使用开发Wordpress主题,我需要制作一个函数来更改单击链接时的一些值,但当我使用div名称制作数组时,数组返回空值 HTML:Jquery 是否可以使用';部门id';作为数组的名称?,jquery,css,arrays,wordpress,frameworks,Jquery,Css,Arrays,Wordpress,Frameworks,请查看此JSFIDLE 这是另一种尝试 我正在使用开发Wordpress主题,我需要制作一个函数来更改单击链接时的一些值,但当我使用div名称制作数组时,数组返回空值 HTML: <a class="button" id="settext1">Some Link</a> <br /> <a class="button" id="settext2">Another Link</a> <br /> <a class="
<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 />
$(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...
}
});
一些链接
另一环节
链接3
jQuery:
<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 />
$(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...
}
});
$(文档).ready(函数(){
//var col_settext1=new Array();//如果我正确地解释了您的问题,那么您的问题是您实际上没有为新数组设置变量名
现阶段:
$myclass = this.id;
$col = 'col_' + $myclass;
$txt = 'txt_' + $myclass;
$chk = 'chk_' + $myclass;
你会的
$col=“col\u id”,
$txt=“txt\u id”,
$chk=“chk\u id”
基本上,这三个变量中的每一个都绑定到一个字符串
现在你可以:
$col = new Array();
$txt = new Array();
$chk = new Array();
因此,这三个变量中的每一个都绑定到一个全新的数组,其中没有任何条目
您要做的是:
$col = new Array();
$col[0] = "col_" + $myclass;
这将创建一个新数组,其中有一个条目“col_id”,绑定到变量名$col
这将为您提供包含id名称的名为$col、$txt和$chk的数组
相反,如果您希望实际创建动态变量名(不确定是否确实需要,但确定),则需要使用。如果我正确理解您的问题(即,您希望基于某些参数创建动态变量名),则应使用以下代码,而不是“$col=new Array();”
等等看起来很简单,我对jquery的了解很少,我花了整整两天的时间试图让它工作,但什么都没有。谁知道这件事只有五分钟。我创建了另一个代码,我需要的只是让警报返回所需的值,然后我可以将我的原始代码投入工作
单击按钮1,我会看到警报“文本1”,但什么也没发生
html
<a class="button" id="arr1">Button1</a>
<a class="button" id="arr2">Button2</a>
<a class="button" id="arr3">Button3</a>
我找到了这个
但我也不能让它工作
我尝试使用另一个,但我不能让它也工作。我在jQuery论坛上询问,最终得到了解决方案
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>
谢谢您的帮助。我需要使用与DIV id同名的数组,数组已经存在,数组的值也已经存在,我需要做的只是单击带有id的链接并使用与id同名的数组,但不起作用…请查看此代码以更好地理解,在出现消息“value return blank…”的窗口中。。。,我需要显示数组中的值,但不返回值:(几乎是这样,但这样会显示数组丢失了,我需要保留它在数组中的值,并根据单击的div的名称调用数组,为了更好地理解您可以查看此代码,请注意,最后一个窗口没有返回数组中的值,这就是问题所在。在这种情况下,您可以简单地将字符串附加到您需要的内容在数组中已经有了,或者,在使用窗口技术(如果您熟悉的话,类似于Python中的Globals())之后,使用div id的专有名称创建新数组,然后将数据复制到其中。是的,这正是我所想的,但我有200多个不同值的数组, 因此,我只想使用一个函数,而不是用几乎相同的代码执行200个函数。我不确定这两种情况中的哪一种,但无论如何,我认为最好编写一个函数,它接受数组和名称,并将两者绑定。你不必做200次同样的事情。
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);
});
});