admin管理员组文章数量:1122832
I have an html project to which I need to add 4 buttons filled from a JSON file using an array. The parameters are taken depending on the link id.
<div class="d-flex justify-content-around mt-4">
<a href="#" id="button-1" class="btn btn-primary">
<img src="" alt="Icon" id="icon-button-1"> Button 1
</a>
<a href="#" id="button-2" class="btn btn-primary">
<img src="" alt="Icon" id="icon-button-2"> Button 2
</a>
<a href="#" id="button-3" class="btn btn-primary">
<img src="" alt="Icon" id="icon-button-3"> Button 3
</a>
<a href="#" id="button-4" class="btn btn-primary">
<img src="" alt="Icon" id="icon-button-4"> Button 4
</a>
</div>
</div>
$(document).ready(function () {
const urlParams = new URLSearchParams(window.location.search);
const repoId = urlParams.get('id');
$.getJSON('repositories.json', function (data) {
const repo = data.find(r => r.id === repoId);
if (repo.buttons && Array.isArray(repo.buttons)) {
repo.buttons.forEach((button, index) => {
const btn = $(`#button-${index + 1}`);
if (btn.length > 0) {
btn.attr('href', button.link || '#');
btn.find('img').attr('src', button.icon || 'default-icon.png');
btn.find('span').text(button.text || `Button ${index + 1}`);
}
});
}
});
</script>
</body>
</html>
Code from JSON file:
[
{
"id": "repo1",
"buttons": [
{
"link": "exmpl.html",
"icon": "exmpl.png",
"text": "btn exmpl1"
},
{
"link": "exmpl.html",
"icon": "exmpl.png",
"text": "btn exmpl2"
},
{
"link": "exmpl.html",
"icon": "exmpl.png",
"text": "btn exmpl3"
},
{
"link": "exmpl.html",
"icon": "exmpl.png",
"text": "btn exmpl4"
}
]
},
{
"id": "repo2",
"buttons": [
{
"link": "exmpl.html",
"icon": "exmpl.png",
"text": "btn exmpl1"
},
{
"link": "exmpl.html",
"icon": "exmpl.png",
"text": "btn exmpl2"
},
{
"link": "exmpl.html",
"icon": "exmpl.png",
"text": "btn exmpl3"
},
{
"link": "exmpl.html",
"icon": "exmpl.png",
"text": "btn exmpl4"
}
]
}
]
I tried renaming repo.buttons to repoId.buttons, but it didn't help (fixed the error in the console, but the problem remained)
Console error: Uncaught ReferenceError: repo is not defined
I have an html project to which I need to add 4 buttons filled from a JSON file using an array. The parameters are taken depending on the link id.
<div class="d-flex justify-content-around mt-4">
<a href="#" id="button-1" class="btn btn-primary">
<img src="" alt="Icon" id="icon-button-1"> Button 1
</a>
<a href="#" id="button-2" class="btn btn-primary">
<img src="" alt="Icon" id="icon-button-2"> Button 2
</a>
<a href="#" id="button-3" class="btn btn-primary">
<img src="" alt="Icon" id="icon-button-3"> Button 3
</a>
<a href="#" id="button-4" class="btn btn-primary">
<img src="" alt="Icon" id="icon-button-4"> Button 4
</a>
</div>
</div>
$(document).ready(function () {
const urlParams = new URLSearchParams(window.location.search);
const repoId = urlParams.get('id');
$.getJSON('repositories.json', function (data) {
const repo = data.find(r => r.id === repoId);
if (repo.buttons && Array.isArray(repo.buttons)) {
repo.buttons.forEach((button, index) => {
const btn = $(`#button-${index + 1}`);
if (btn.length > 0) {
btn.attr('href', button.link || '#');
btn.find('img').attr('src', button.icon || 'default-icon.png');
btn.find('span').text(button.text || `Button ${index + 1}`);
}
});
}
});
</script>
</body>
</html>
Code from JSON file:
[
{
"id": "repo1",
"buttons": [
{
"link": "exmpl.html",
"icon": "exmpl.png",
"text": "btn exmpl1"
},
{
"link": "exmpl.html",
"icon": "exmpl.png",
"text": "btn exmpl2"
},
{
"link": "exmpl.html",
"icon": "exmpl.png",
"text": "btn exmpl3"
},
{
"link": "exmpl.html",
"icon": "exmpl.png",
"text": "btn exmpl4"
}
]
},
{
"id": "repo2",
"buttons": [
{
"link": "exmpl.html",
"icon": "exmpl.png",
"text": "btn exmpl1"
},
{
"link": "exmpl.html",
"icon": "exmpl.png",
"text": "btn exmpl2"
},
{
"link": "exmpl.html",
"icon": "exmpl.png",
"text": "btn exmpl3"
},
{
"link": "exmpl.html",
"icon": "exmpl.png",
"text": "btn exmpl4"
}
]
}
]
I tried renaming repo.buttons to repoId.buttons, but it didn't help (fixed the error in the console, but the problem remained)
Console error: Uncaught ReferenceError: repo is not defined
Share Improve this question asked Nov 21, 2024 at 15:27 ne slava marlowne slava marlow 1 9 | Show 4 more comments1 Answer
Reset to default 0Consider the following: https://jsfiddle.net/Twisty/vax2e167/
HTML
<div>
<div class="d-flex justify-content-around mt-4">
<a href="#" id="button-1" class="btn btn-primary">
<img src="" alt="Icon" id="icon-button-1" /> <span>Button 1</span>
</a>
<a href="#" id="button-2" class="btn btn-primary">
<img src="" alt="Icon" id="icon-button-2" /> <span>Button 2</span>
</a>
<a href="#" id="button-3" class="btn btn-primary">
<img src="" alt="Icon" id="icon-button-3" /> <span>Button 3</span>
</a>
<a href="#" id="button-4" class="btn btn-primary">
<img src="" alt="Icon" id="icon-button-4" /> <span>Button 4</span>
</a>
</div>
</div>
JavaScript
var myData =
'[{"id":"repo1","buttons":[{"link":"exmpl.html","icon":"exmpl.png","text":"btn exmpl1"},{"link":"exmpl.html","icon":"exmpl.png","text":"btn exmpl2"},{"link":"exmpl.html","icon":"exmpl.png","text":"btn exmpl3"},{"link":"exmpl.html","icon":"exmpl.png","text":"btn exmpl4"}]},{"id":"repo2","buttons":[{"link":"exmpl.html","icon":"exmpl.png","text":"btn exmpl1"},{"link":"exmpl.html","icon":"exmpl.png","text":"btn exmpl2"},{"link":"exmpl.html","icon":"exmpl.png","text":"btn exmpl3"},{"link":"exmpl.html","icon":"exmpl.png","text":"btn exmpl4"}]}]'
$(function () {
//var urlParams = new URLSearchParams(window.location.search);
//var repoId = urlParams.get("id")
var repoId = "repo2"
$.post("/echo/json/", { json: myData }, function (data) {
var repo = data.find((r) => r.id === repoId)
if (repo.buttons && Array.isArray(repo.buttons)) {
repo.buttons.forEach((button, index) => {
var btn = $("#button-" + (index + 1))
if (btn.length > 0) {
btn.attr("href", button.link || "#")
btn.find("img").attr("src", button.icon || "default-icon.png")
btn.find("span").text(button.text || `Button ${index + 1}`)
}
})
}
})
})
In this example, <span>
tags had to be added to the HTML to make your Script work properly. JSFiddle is a good way to test mock JSON as it has an Echo feature.
本文标签: javascriptI can39t add buttons with filling from JSON fileStack Overflow
版权声明:本文标题:javascript - I can't add buttons with filling from JSON file - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736309450a1934027.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
repoId
was not found? What do you mean "the rest of the information is loaded"? Loaded where? – mykaf Commented Nov 21, 2024 at 15:46data.find(...)
, "if no values satisfy the testing function,undefined
is returned" (per MDN). It won't throw an error. – mykaf Commented Nov 21, 2024 at 15:47