简体   繁体   中英

How to print value in array with jQuery - .html

I am trying to print a value from an array in jQuery to the screen using the .html function. The variable is test.

If I just use the line of code below it will print everything in the array:

$(this).html(test).addClass('messageboxerror').fadeTo(900,1);

Array ( [0] => 2820 Prairie [1] => 19316 [2] => 2820 Prairie [3] => Beloit [4] => 53511 [5] => [6] => 2012-01-17 [7] => [8] => union [9] => Rock County )

I have tried putting .html(test[0]) but this broke the script.

Thanks,

Edit : Check this post on how to send JSON data.

Arrays in javascript is constructed as below,

var test = ["2820 Prairie", "19316", "2820 Prairie", "Beloit", "53511", "", "2012-01-17", "", "union", "Rock County"];

Now test[0] will return you "2820 Prairie".

MDN Array Object Reference

 var colors = ['#ff9e2a', '#2b908f', '#96c18c', "#ff3232", '#09a43e', '#7798BF', '#3ef3cd', '#ff0066', '#55BF3B', '#DF5353', '#7798BF', '#aaeeee', '#DDDF0D', '#058DC7', '#50B432', '#ED561B', '#DDDF00', '#24CBE5', '#90ee7e', '#FF9655', '#FFF263', '#6AF9C4', '#082a50', '#f7a35c', '#e9625e', '#dce405', '#a1034d', '#42A07B', '#9B5E4A', '#2e898e', '#82914E', '#7d7c7c', '#6ecfdf', '#f45b5b', '#aaeeee', '#ad4029', '#64E572', '#7cb5ec', '#8085e9', '#8d4654', '#514F78', '#72727F', '#1F949A', '#86777F', '#eeaaee', '#c62a2b' ] $(document).ready(function() { for (i = 0; i < colors.length; i++) { console.log(colors[i]); $('#ColorArrayThemeA').append('<li class="bgclr' + i + '"><span>' + colors[i] + '</span></li>'); $('.bgclr' + i + '').css({ "background-color": colors[i] }); //$('#ColorArrayThemeA').css({ "background-color": colors[i] }) } }); 
 ol#ColorArrayThemeA li { margin: 5px 20px; float: left; padding: 5px 0px; width: 20%; text-align: center; font-weight: bold; font-family: Arial; } ol#ColorArrayThemeA li span { background-color: white; padding: 2px 5px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ol id="ColorArrayThemeA"></ol> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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