我正在研究一个问答比较示例,我试图将问题字符串与json数据进行比较。问题和答案将作为json数据提取。你能帮我指导解决比较问题。
<label for="What_is_planning">What is planning</label>
<br>
<label for="What_is_implementing">What is implementing</label>
<br>
<div class="result"></div>
$(document).ready(function () {
$('label').mouseover(function () {
var helpString = this.firstChild.nodeValue.replace(':', '').trim();
var answerString = "<br>No Answer";
$(".result").html(" <hr><b>Guest: </b>" + helpString);
$(".result").append(" <div><b>AskGFS: </b>" + answerString);
console.log(helpString);
});
var QandAData = '{"gfsdata":[{"question":"what is planning","answer":"This is a important part of life"},' +
'{"question":"what is implementing","answer":"this is followed by planning"}]}';
//Comparing answers
for (var i = 0; i < QandAData.length; i++) {
if (QandAData[i].gfsdata.question == 'what is planning') {
answerString = QandAData[i].gfsdata.answer;
}
}
});
答案 0 :(得分:1)
尝试:
$(document).ready(function () {
$('label').mouseover(function () {
var helpString = this.firstChild.nodeValue.replace(':', '').trim();
var answerString = "<br>No Answer";
var QandAData = {
"gfsdata": [{
"question": "What is planning",
"answer": "This is a important part of life"
}, {
"question": "What is implementing",
"answer": "This is followed by planning"
}]
};
for (var i = 0; i < QandAData.gfsdata.length; i++) {
if (QandAData.gfsdata[i].question == helpString) {
answerString = QandAData.gfsdata[i].answer;
}
}
$(".result").html(" <hr><b>Guest: </b>" + helpString+" <div><b>AskGFS: </b>" + answerString);
});
});