jquery - 如何在 JQuery 中使用每个并在 html 表中显示
问题描述
当前的问题是我的电影标题的完整列表显示在我表的第一行中。我要他们分开。我需要以某种方式将它们分解成单独的元素,但我似乎找不到任何好的例子来工作。(仅供参考,我是 JavaScript/JQuery 的新手)
<body>
<table id="content">
<tr>
<td></td>
</tr>
</table>
<form>
TITLE:<br>
<input type="text" id="inputTitle" value="" placeholder="Title"><br>
RELEASE DATE:<br>
<input type="text" id="inputRelease" value="" placeholder="Release"><br>
DIRECTOR:<br>
<input type="text" id="inputDirector" value="" placeholder="Director"><br>
GENRE:<br>
<input type="text" id="inputGenre" value="" placeholder="Genre"><br>
</form>
<br><br>
<button id='button'>Add Movie</button>
<script>
$(document).ready(function(){
$.get("https://moviesfrontandback.herokuapp.com/api/movies/", function(data){
var i;
for (i = 0; i < data.length; i++) {
$('#content').append(data[i].title)
}
});
$('#button').click(function(){
title = $('#inputTitle').val()
releaseYear = $('#inputRelease').val()
director = $('#inputDirector').val()
genre = $('#inputGenre').val()
WriteToAPI()
})
WriteToAPI = function(){
$.post('https://moviesfrontandback.herokuapp.com/api/movies/', {
title: title,
releaseYear: releaseYear,
director: director,
genre: genre,
}, function(data, status){
alert("Title:\t\t" + title + "\nRelease:\t" + releaseYear + "\nDirector:\t" + director + "\nGenre:\t\t" + genre)
} )
}
});
</script>
</body>
</html>
解决方案
您需要为表格设置 html,而不仅仅是附加标题。像这样的东西...
$.get("https://moviesfrontandback.herokuapp.com/api/movies/", function(data){
var html = '';
for (var i = 0; i < data.length; i++) {
//Append a new row
html += '<tr><td>' + data[i].title + '</td></tr>'
}
//Overwrite the table html
$('#content').html(html);
});
推荐阅读
- python - PDB 可以使用 Python3.5+ 类型的注释/提示进行自动完成吗?
- swift - 以编程方式触发 unwind segue
- c# - C# 将 DataSet 绑定到 Windows 窗体应用程序中的 DataGridView
- swift - 如何使用 segue firebase 发送数据?
- ios - 读取文件内容不使用 FileManager
- python - 如何将 TensorFlow 2D 卷积 (tf.nn.conv2d) 应用于单个(非批处理)2D 图像?
- c# - 带有静态类的自动实现属性的这条线是什么意思?
- android - 在代码中使用变量或在android中定义字符串资源
- python - 您如何绘制按另一列分组的数据?
- algorithm - 为什么答案不是 O(n^2)?