使用复选框从表中选择时添加两次数据

我不确定这里的循环有什么问题。 当我使用复选框从表中选择数据(使用JSON生成)时,会多次添加所选数据。

这里的代码有什么问题,它迭代循环中的所有复选框,并选择要在articledata中添加的选中值。

$('input[name="articles"]:checked').each(function() { var articleJson = JSON.parse(decodeURIComponent(this.value)); articlesResult +='
'+articleJson.PublishDate+'
'+articleJson.newsJobFuntion+'
'+articleJson.newsJobFuntionGA+','+articleJson.newsJobFuntionGA+','+articleJson.newsJobLevelGA+'
' $('.box').append(articlesResult); });

我认为你需要使用HTML而不是追加。 因为append将每次添加已检查的数据以及当前已经具有您之前选择的数据的当前数据

 $('input[name="articles"]:checked').each(function() { var articleJson = JSON.parse(decodeURIComponent(this.value)); articlesResult +='
'+articleJson.PublishDate+'
'+articleJson.newsJobFuntion+'
'+articleJson.newsJobFuntionGA+','+articleJson.newsJobFuntionGA+','+articleJson.newsJobLevelGA+'
' }); $('.box').html(articlesResult);

试试这个,你将同一组模板一遍又一遍地添加到同一个变量并将其附加到DOM。 你只需要在循环后调用一次append。

 $('input[name="articles"]:checked').each(function() { var articleJson = JSON.parse(decodeURIComponent(this.value)); articlesResult +='
'+articleJson.PublishDate+'
'+articleJson.newsJobFuntion+'
'+articleJson.newsJobFuntionGA+','+articleJson.newsJobFuntionGA+','+articleJson.newsJobLevelGA+'
' }); $('.box').append(articlesResult);
Interesting Posts