我想要实现下一个目标:我有从我的JSON文件中获取的单词。我想根据插入JSON文件中的音节数量创建输入。这就是我的JSON的样子(让你理解我对音节的意思)
{
"main_object": {
"id": "new",
"getExerciseTitle": "ExampleForStackOverflow",
"language": "nl_NL",
"application": "lettergrepen",
"main_object": {
"title": "ExampleForStackOverflow",
"language": "nl_NL",
"exercises": [
{
"word": "Example",
"syllables": [
"Example1",
"Example2"
]
}
]
},
"dataType": "json"
}
}
所以,我想创建输入字段等于我插入的与单词匹配的音节数量(如果我有更多的单词与X量的音节那么那些应该有自己的输入字段,旁边这个单词)。我做了一些研究,发现了我可以使用的exercise.syllables.join(' ')
之类的东西。然而,这些用syllables
获取了这个词,但是我不希望看到syllables
,所以这可能是另一种方式吗?
现在我有这个:
var fakejson = { // extrapolating this based on the code
main_object: {
main_object: {
exercises: [{
word: "one"
},
{
word: "two"
},
{
word: "three"
}
]
}
}
}
function createExercise(json) {
const exercises = json.main_object.main_object.exercises;
exercises.forEach(function(exercise) {
var exer = $('<div/>', {
'class': 'row'
})
.append(
$('<div/>', {
'class': 'col-md-3'
})
.append(
$('<div/>', {
'class': 'row'
})
.append($('<div>', {
class: 'col-md-3 audioButton'
}))
.append($('<div>', {
class: 'col-md-9 ExerciseWordFontSize exerciseWord',
'id': 'wordInput[' + ID123 + ']',
text: exercise.word
}))
)
).append(
$('<div>', {
class: 'col-md-9 inputSyllables'
// text: "(4 x col-3 here)"
})
);
$("#exerciseField").append(exer);
ID123++;
exer.find('.audioButton').append(getAudioForWords());
exer.find('.inputSyllables').append(inputFieldsForSyllables());
});
}
createExercise(fakejson);
function inputFieldsForSyllables() {
var getInputField = $('<input/>', {
'type': 'text',
'class': 'form-group col-md-3',
'name': 'inputSyllables'
});
return getInputField;
}
我也想知道这一点 - &gt;我是否必须为输入字段创建单独的循环,还是可以在第一个循环中创建它?