JSONP ファイルから受け取った JavaScript オブジェクトがあります。
オブジェクトには複数の「オプション」と「結果」が含まれており、ユーザーがクリックしたときにページ上の html を調整するために使用されます。
現在、json ファイルに HTML 文字列 (json 参照を介して挿入) が存在するかどうかを確認できます。私がしたいのは、その文字列を取得し、json ファイルで次の「結果」または「オプション」を見つけてから、その「オプション」または「結果」の値を返して、それを使用して html を変更できるようにすることです...
それ、どうやったら出来るの?.indexOf メソッドを使用して現在のインデックスを見つけようとしましたが、「オプション」のような特定のプロパティを見つけるのに実際には役立ちません。
これは、JSONP ファイルを反復処理し、現在の文字列が存在するかどうかを確認するために使用しているコードです。
$.ajax({
url: "http://www.myurl.com/jsonp.php",
type: "GET",
dataType: "jsonp",
jsonpCallback: "otmjsonp",
async: false,
success: function (JSON) {
$(".result").on("click", function () {
var currentResult = $(this).text(); //.result is the line of HTML the user has clicked
for (var playerSelection in JSON) {
if (JSON.hasOwnProperty(playerSelection)) {
if (JSON[playerSelection] === currentResult) {
alert("this selection exists in the JSON");
}
}
}
})
}
});
そして、これは大きな JSONP ファイルの非常に単純なバージョンです。
otmjsonp({
"situation1" : "Your opponent is trying to tackle you", "playerPrompt1" : "What will you do first?",
"option1" : "avoid him",
"result1" : "he tackles you",
"situation2" : "you were tackled", "playerPrompt2" : "Your opponent begins to slow down",
"option2" : "chase after him",
"result2" : "you caught up",
)}
などなど
私は完全に立ち往生しているので、漠然としたアイデア/方向性でもありがたいです。