英文:
How to Generate Row & Columns with load data by JQuery?
问题
我想使用jQuery的each函数重复生成带有数据的行和列。请帮忙。
所需的HTML结构和jQuery尝试:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var data = [{
"name": "Alice",
"surname": "Smith",
"rollnum": "00002"
},
{
"name": "Bob",
"surname": "Walter",
"rollnum": "00004"
}
];
load();
function load() {
var name = document.querySelector(".name");
var surname = document.querySelector(".surname");
$.each(data, function(index, value) {
var $name = value.name;
var $surname = value.surname;
var $rollnum = value.rollnum;
console.log($name);
console.log($surname);
console.log($name);
$(".name").text($name);
$(".surname").text($surname);
$(".rollnum").text($rollnum);
});
}
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<div class="eventsBlock">
<div class="row">
<div class="col-md-4 col-sm-4">
<span class="name">name</span>
</div>
<div class="col-md-4 col-sm-4">
<span class="surname">surname</span>
<span class="rollnum">rollnum</span>
</div>
<div class="col-md-4 col-sm-4">
<span class="locaton">locaton</span>
</div>
</div>
</div>
<!-- end snippet -->
链接:https://jsfiddle.net/anoopsuda/3a6ucg4d/3/
英文:
I want to generate row and column repeatably with data by jquery each function. Please help.
Required HTML Structure and jquery attempt:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var data = [{
"name": "Alice",
"surname": "Smith",
"rollnum": "00002"
},
{
"name": "Bob",
"surname": "Walter",
"rollnum": "00004"
}
];
load();
function load() {
var name = document.querySelector(".name");
var surname = document.querySelector(".surname");
$.each(data, function(index, value) {
var $name = value.name;
var $surname = value.surname;
var $rollnum = value.rollnum;
console.log($name);
console.log($surname);
console.log($name);
$(".name").text($name);
$(".surname").text($surname);
$(".rollnum").text($rollnum);
});
}
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<div class="eventsBlock">
<div class="row">
<div class="col-md-4 col-sm-4">
<span class="name">name</span>
</div>
<div class="col-md-4 col-sm-4">
<span class="surname">surname</span>
<span class="rollnum">rollnum</span>
</div>
<div class="col-md-4 col-sm-4">
<span class="locaton">locaton</span>
</div>
</div>
</div>
<!-- end snippet -->
答案1
得分: 2
你需要使用.append()
方法,并且需要在surname
和rollnumber
附近稍微改变HTML结构(按照我的建议将它们放在单独的div中)。
var data = [{
"name": "Alice",
"surname": "Smith",
"rollnum": "00002"
},
{
"name": "Bob",
"surname": "Walter",
"rollnum": "00004"
}
];
load();
function load() {
$.each(data, function(index, value) {
$('.eventsBlock').append(`<div class="row">
<div class="col-md-4 col-sm-4">
<span class="name">name: ` + value.name + `</span>
</div>
<div class="col-md-4 col-sm-4">
<span class="surname">surname: ` + value.surname + `</span>
</div>
<div class="col-md-4 col-sm-4">
<span class="rollnum">rollnum: ` + value.rollnum + `</span>
</div>
<div class="col-md-4 col-sm-4">
<span class="locaton">locaton: N/A</span>
</div>
</div><br>`);
});
}
如果你使用下面的表格HTML会更好:
var data = [{
"name": "Alice",
"surname": "Smith",
"rollnum": "00002"
},
{
"name": "Bob",
"surname": "Walter",
"rollnum": "00004"
}
];
load();
function load() {
$.each(data, function(index, value) {
$('.eventsBlock tbody').append(`<tr>
<td>` + value.name + `</td>
<td>` + value.surname + `</td>
<td>` + value.rollnum + `</td>
<td>N/A</td>
</tr>`);
});
}
希望对你有帮助!
英文:
You need to use .append()
for this and need to change HTML structure a bit near surname
and rollnumber
(put them in separate div as per my suggestion)
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var data = [{
"name": "Alice",
"surname": "Smith",
"rollnum": "00002"
},
{
"name": "Bob",
"surname": "Walter",
"rollnum": "00004"
}
];
load();
function load() {
$.each(data, function(index, value) {
$('.eventsBlock').append(`<div class="row">
<div class="col-md-4 col-sm-4">
<span class="name">name: ` + value.name + `</span>
</div>
<div class="col-md-4 col-sm-4">
<span class="surname">surname: ` + value.surname + `</span>
</div>
<div class="col-md-4 col-sm-4">
<span class="rollnum">rollnum: ` + value.rollnum + `</span>
</div>
<div class="col-md-4 col-sm-4">
<span class="locaton">locaton: N/A</span>
</div>
</div><br>`);
});
}
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<div class="eventsBlock">
</div>
<!-- end snippet -->
It would be much better if you use table HTML like this:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var data = [{
"name": "Alice",
"surname": "Smith",
"rollnum": "00002"
},
{
"name": "Bob",
"surname": "Walter",
"rollnum": "00004"
}
];
load();
function load() {
$.each(data, function(index, value) {
$('.eventsBlock tbody').append(`<tr>
<td>` + value.name + `</td>
<td>` + value.surname + `</td>
<td>` + value.rollnum + `</td>
<td>N/A</td>
</tr>`);
});
}
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<table class="eventsBlock">
<thead>
<tr>
<th>name</th>
<th>surname</th>
<th>rollnum</th>
<th>locaton</th>
<tr>
</thead>
<tbody>
</tbody>
</table>
<!-- end snippet -->
答案2
得分: 1
这段代码是使用jQuery编写的。以下是翻译好的代码:
<div class="eventsBlock">
<div class="row">
<div class="col-md-4 col-sm-4">
<span class="name">姓名</span>
</div>
<div class="col-md-4 col-sm-4">
<span class="surname">姓氏</span>
<span class="rollnum">学号</span>
</div>
<div class="col-md-4 col-sm-4">
<span class="locaton">地点</span>
</div>
</div>
</div>
<div class="eventsRows">
<!--内容将在这里显示-->
</div>
var data = [
{
"name": "Alice",
"surname": "Smith",
"rollnum": "00002",
"locaton": "孟买"
},
{
"name": "Bob",
"surname": "Walter",
"rollnum": "00004",
"locaton": "德里"
}
];
function load(){
let allRows = '';
$.each(data, function (index, value) {
var $name = value.name;
var $surname = value.surname;
var $rollnum = value.rollnum;
var $locaton = value.locaton;
let rowView = `<div class="row">
<div class="col-md-4 col-sm-4">
<span class="name">${$name}</span>
</div>
<div class="col-md-4 col-sm-4">
<span class="surname">${$surname}</span>
<span class="rollnum">${$rollnum}</span>
</div>
<div class="col-md-4 col-sm-4">
<span class="locaton">${$locaton}</span>
</div>
</div>`;
allRows += rowView;
});
$('.eventsRows').html(allRows);
}
load();
请注意,这只是代码的翻译部分,不包括任何其他内容。
英文:
This works if you're using jQuery.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var data = [
{
"name":"Alice",
"surname":"Smith",
"rollnum":"00002",
"locaton":"Mumbai"
},
{
"name":"Bob",
"surname":"Walter",
"rollnum":"00004",
"locaton":"Delhi"
}
];
function load(){
let allRows = ''
$.each(data, function (index, value) {
var $name = value.name;
var $surname = value.surname;
var $rollnum = value.rollnum;
var $locaton = value.locaton;
let rowView = `<div class="row">
<div class="col-md-4 col-sm-4">
<span class="name">${$name}</span>
</div>
<div class="col-md-4 col-sm-4">
<span class="surname">${$surname}</span>
<span class="rollnum">${$rollnum}</span>
</div>
<div class="col-md-4 col-sm-4">
<span class="locaton">${$locaton}</span>
</div>
</div>
</div>`
allRows += rowView
});
$('.eventsRows').html(allRows)
}
load();
<!-- language: lang-html -->
<div class="eventsBlock">
<div class="row">
<div class="col-md-4 col-sm-4">
<span class="name">name</span>
</div>
<div class="col-md-4 col-sm-4">
<span class="surname">surname</span>
<span class="rollnum">rollnum</span>
</div>
<div class="col-md-4 col-sm-4">
<span class="locaton">locaton</span>
</div>
</div>
</div>
<div class="eventsRows">
<!--Content will go here-->
</div>
<!-- end snippet -->
答案3
得分: 1
你需要在循环中使用一个变量。
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var data = [{
"name": "Alice",
"surname": "Smith",
"rollnum": "00002"
},
{
"name": "Bob",
"surname": "Walter",
"rollnum": "00004"
}
];
load();
function load() {
var output = '';
var name = document.querySelector(".name");
var surname = document.querySelector(".surname");
$.each(data, function(index, value) {
var $name = value.name;
var $surname = value.surname;
var $rollnum = value.rollnum;
console.log($name + ' - ' + $surname + ' - ' + $rollnum);
output += '<div class="row"><div class="col-md-4 col-sm-4"><span class="name">' + $name + '</span></div><div class="col-md-4 col-sm-4"><span class="surname">' + $surname + '</span><span class="rollnum">' + $rollnum + '</span></div><div class="col-md-4 col-sm-4"><span class="locaton">locaton</span></div></div>';
});
$('.eventsBlock').html(output);
}
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="eventsBlock"></div>
<!-- end snippet -->
英文:
You need to use a var in your loop
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var data = [{
"name": "Alice",
"surname": "Smith",
"rollnum": "00002"
},
{
"name": "Bob",
"surname": "Walter",
"rollnum": "00004"
}
];
load();
function load() {
var output = '';
var name = document.querySelector(".name");
var surname = document.querySelector(".surname");
$.each(data, function(index, value) {
var $name = value.name;
var $surname = value.surname;
var $rollnum = value.rollnum;
console.log($name + ' - ' + $surname + ' - ' + $rollnum);
output += '<div class="row"><div class="col-md-4 col-sm-4"><span class="name">' + $name + '</span></div><div class="col-md-4 col-sm-4"><span class="surname">' + $surname + '</span><span class="rollnum">' + $rollnum + '</span></div><div class="col-md-4 col-sm-4"><span class="locaton">locaton</span></div></div>';
});
$('.eventsBlock').html(output);
}
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="eventsBlock"></div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论