如何在javascript / jquery中分割逗号,使每个单词进入各自的输入字段

是的,我已经完成了我的研究并发现了这个: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split或可能重复的人可以参考: 如何使用JavaScript拆分逗号分隔的字符串?

但是现在我的问题是:当我取出我的syllables (我会回到原来的那个)我最终会在每个单词之间使用逗号。 我想逃避这些逗号,以便syllables进入他们自己的输入字段,而不是在一个输入字段中成为一个字符串。

这就是我的循环现在的样子:

  $(document).ready(function () { $.getJSON('json_files/jsonData_' + ID + '.json', function(json) { //Fetches the correct exercise title from the database and places it inside the header var exercisetitle = json.main_object.getExerciseTitle; $("#getExerciseTitle").val(exercisetitle); //loops through the words and fetches them back to the CMS side. var exercise = json.main_object.main_object.exercises; $.map(exercise, function(exercise, i) { $("#addOpdracht").click(); $(".exerciseGetWordInput_" + i).val(exercise.word) // starts with 0 console.log(exercise.syllables); $(".sylll" + i).val(exercise.syllables) }); }); }); 

但是当我使用这个循环时,我最终得到一个输入字段中的单词,其间有逗号,因此它会取回一个输入字段中的所有单词。

我的JSON如何:

 { "main_object": { "id": "new", "getExerciseTitle": "TestToConfirmMoreWords", "language": "nl_NL", "application": "lettergrepen", "main_object": { "title": "TestToConfirmMoreWords", "language": "nl_NL", "exercises": [ { "word": "HACCP", "syllables": [ "1", "2", "", "" ] }, { "word": "HACCP-normen", "syllables": [ "123", "123", "123", "" ] } ] }, "dataType": "json" } } 

这是一张图片,因此可视化更容易:

在此处输入图像描述

正如你所看到的那样,它将“1,2 ,,”放在一个输入字段而不是2个输入字段,而最后2个输入字段为空。 它也只是在一个输入字段下面堆叠“123,123,123”,而不是将它放在右侧的运动字段中,每个输入字段中都有它们。 最后一个应该是空的,因为那是一个“空”字符串。

是我在寻找错误的方向(看看splitfunction,而不能解决问题)或者是我错过的其他东西?