Monday 15 April 2013

jquery - Finding properties in a javascript object -


I have a JavaScript object, which I received from a JSONP file.

Multiple "options" and "results" in the object, which is used to adjust the html on the page, when the user clicks.

Right now, I am able to check whether the HTML string (inserted via json reference) is present in the JSO file or not. What I want to do is take the string, find the next "result" or "option" in the json file, and then return that "option" or "result" value so that I can use it to change html ... < / P>

How can I do this? I'm trying the .indexOf method to find the current index, but it does not really help me in finding a particular option such as "options".

This is the code that I am using to reproduce through the JSONP file and it is finding that the current string exists.

  $ Type: "GET", datatype: "jsonp", jsonpCallback: "otmjsonp", async: wrong, success: function (JSON) {Ajax ({url: "http: /www.myurl.com/jsonp.php") $ ("Result."). ("Click", function () {var currentResult = $ (this) .text (); //.result is the line of HTML that the user clicked (var player selection in JSON) {If (JSON.hasOwnProperty (playerSelection)} {If (JSON [playerSelection] === currentResult) {Warning ("This selection is present in JSON")}}}}}}}});   

And here is a very simple version of the large JSONP file:

  otmjsonp ({"situation1": "Your opponent is trying to deal with you "", "Option 1": "Avoid it", "Result 1": "He tackles you", "Status2": "Problem 1": "You were bothered", " PlayerPrompt2 ":" Your opponent begins to slow down "," option 2 ":" followed by chase "," result 2 ":" you caught ",}}   

Etc. etc. Fuzzy thoughts / instructions Would be appreciated because I was completely trapped.

One of the issues The part here is that you have added your UI combinations with your data initialization. I think that you really want to do this that the JSON request is to separate the data from handling clicks.

 < Code> $ (function () {var setup HTML, handleClick, updateHTML, original data, resultsData; updated HTML = function (JSON) {// html Add or activate a ML at which person $ ('. ') .text ()}; Handleclick = function (e) {var currChoice = $ (e.target) .text (); If (resultsData === undefined) {e.stopPropagation (); E.preventDefault (); For return;} (result.data in var ps) {if (resultData.hasOwnProperty (ps) & amp; amp; and data data [ps] === currChoice) {resultData = resultData [ps]; UpdateHTML (resultData);}}} $ ('.result') ('click', handleClick) $ .ajax ({url: "http://www.myurl.com/jsonp.php", type: " GET ", datatype:" jsonp ", jsonpCallback:" Otmjsonp ", async: false, success: function (data) {resultData = origData = data; // UI view setup HTML (JSON);}})}};    

No comments:

Post a Comment