开发者

how to get these two array values using jquery

开发者 https://www.devze.com 2023-01-04 14:23 出处:网络
I have var 1starray = [1,2,3,4,5,6]; var 2ndarray = [A,B,C,D]; Using jQuery I need to show these two array 开发者_JS百科values on click or on change function? Within alertbox?

I have

var 1starray = [1,2,3,4,5,6];
var 2ndarray = [A,B,C,D];

Using jQuery I need to show these two array 开发者_JS百科values on click or on change function? Within alertbox?

Can anybody help me out?


var a = ['a','b','c'];

$('selector').click(function(){

  $.each(a, function(i, obj) {
    alert(obj);
  });

});

repeat for second unless you want to merge the two arrays and then display them. Then you would do something like this

var a = ['a','b','c'];
var b = ['1','2','3'];

var c = $.merge(a,b);    
$('selector').click(function(){    
  $.each(c, function(i, obj) {
    alert(obj);
  });
});


Another possible option would be to use array functions which are available in any ECMAScript 3 or above implementation.

alert(a.concat(b).join());


Like this....

$('selector').click(function(){
  alert(1starray[index]);
});

You need to replace index with whatever index eg 1, 2, 3, A, B


if i understand you currectly, you need to show [1,2,3,4,5,6,A,B,C,D]! in this case let's create new array, with all elements of first and second array.

    var array1 = [1,2,3,4,5,6];
    var array2 = ['A','B','C','D'];
    var size1 = array1.length;
    var size2 = array2.length;
    var size3 = size1 + size2;
    var array3 = new Array(size3);
    for(var i=0; i< size1;i++)
    {
        array3[i] = array1[i];
    }
    for(var j=0; j< size2;j++)
    {
        array3[i+j] = array2[j];
    }
    alert(array3);//[1,2,3,4,5,6,A,B,C,D]

but it's javascript, not jquery;)

0

精彩评论

暂无评论...
验证码 换一张
取 消