How do I convert a JSON object to an array for use in jQuery? -


I am creating a cost estimator form, to manipulate the Select menu JQuery Uses Basically, when a new volume is selected, the value of each option is multiplied by per-unit value in each selection menu, and a new price is shown to the user.

What I am trying to do is to pull prices per unit from a PHP file, which stores these prices in a series of arrays; Ex:

  & lt ;? Php // prices.php $ colorPrices = array (2.339.3.195,6.537,2.614,2.614,1.75 9); $ Json = json_encode ($ color value); Echo $ json; ? & Gt;  

Keeping the ARAIDs separate will keep my jQuery cleaner and makes updating pricing easier.

In my jQuery file, which calculates the total value, I read it in JSON like this:

  $. GetJSON ('prices.php', function) {var colorArray = data;})  

At this point, color space is an object, so it's not good with this bit of jQuery Plays:

  // Whenever a new color is selected, move through the color arrows and currently selected multiplied by $ ('#color options'). Each (function (i) {$ (this) .attr ('label', qty * colorArray [i]);});  

My thinking is that if I can change the colorArray to an array, then I can loop through its contents. Now, when someone chooses a new color, nothing happens.

Am I close or unaware?

  [2.339, 3.195, 6.537, 2.614, 2.614, 1.75 9 ]  

.

I have put together a to display the data given in the JSON string as an array, to show it. Add / edit to the URL to see the code.

The code from the demo

  $ (function () {$ ('button'). Click (function () {// url just another jsbin.com Page that includes // this string - ["2.339", "3.195", "6.537", "2.614", "2.614", "1.759"] $ .getJSON ('http://jsbin.com/amibo' , Function (data) {$ .each (data, function (i, v) {$ ('& lt; div & gt;') text (v) .appendTo ('body');})}}); });});  

Note that in your example, inside the colorArray function, it is declared locally that AJAX completes the request and is therefore available outside the area Will not done. You can either use the high scoppy variable to capture returned data or to run inside the callback function.

Edit:

I have looked at and you want something like

  $ ('# quantity') . Change (work () {var qty = $ ('option: selected', this) .val (); switch (qty) {case '250': $ .getJSON ('script / jsondata.php', function) {// new update option labels function update option labels (quantity, data); // I'm not sure that you need this part because it seems that // basically replaces new update option labels function // I have made a comment, but I can wrong what you are doing * $ .each (data, function (i, value } {$ ('# Color option'). Eq (i) .attr ('label', qty * value);}); * /}); break; case '500': // ditto, but a different array Brake recovery; // etc ... Default: Warning ("defaults"); breakdown;} // I would like to use the local "color array" variable which will be updated Auction Lobbles (QT, ARR) {$ ('#' Option '). Each (function (i) {$ (this) .attr (' label ', Qty * arr [i]);});}});  

I have introduced a new function to update option labels. The Function executed function is scocked when the change event is & lt; Select & gt; id volume . The variable qty can be used within the callback function for the getJSON () command in each case statement, because it has been declared out of the scope of the callback function. The data variable and volume both updateOptionLabels () are passed in the function so that these codes are not repeated in each case statement. It can probably be refined in advance, I have commented on this $ Every () looks like whatever you were trying to do with it now updateOptionLabels () function

I suggest watching Doug Crockford's excellent, especially the section on functions. Take a look at Mozilla's excellent Core JavaScript 1.5 reference content too.


Comments

Popular posts from this blog

MySql variables and php -

url rewriting - How to implement the returnurl like SO in PHP? -

Which Python client library should I use for CouchdB? -