jQuery - 加载后显示列表
我制作了一个脚本,使用 getJSON()
从 MySQL 数据库中的“父”流派中获取子流派。为了显示子流派列表,我使用了 show()
函数,但由于列表尚未加载,它只会弹出,我不喜欢这样。如何使用 load()
函数(或任何其他对我的情况有帮助的函数)等待列表加载,然后使用 show()
?
function getZvrsti(id){
$.getJSON('test.php?parent='+id, function(data) {
var tmpLi;
$.each(data, function(id, name) {
tmpLi = $('<li><input type="checkbox" value="'+name['id']+'" id="zvrstId'+name['id']+'" /> <label for="zvrstId"'+name['id']+'">'+name['name']+'</label></li>');
$(".drugeZvrsti").append(tmpLi);
tmpLi = "";
});
});
}
这是使用 getJSON
的函数,这是我使用 show()
的片段:
$(".naprejZvrst").click(function(){
if(!on2){
parent = $(this).attr("id");
getZvrsti(parent);
$(".drugeZvrsti").load(function(){ //my poor example of load()
druga.show(400);
});
on2 = true;
}
else if(on2){
druga.hide(400);
on2 = false;
$(".drugeZvrsti").html('');
}
})
类 drugeZvrsti
是 < ;ul>
列表,其中显示列表,var druga
是一个
,其中整个
> 列表是。 注意:我找到了 load()
函数的示例,但没有解释如何使用列表,大多数都是用于图像。I've made a script, to get child genres from a "parent" genre from a MySQL database, using getJSON()
. To show the list of child genres, I used show()
function, but because the list hasn't loaded yet, it will just pop up, and I don't like that. How do I use the load()
function (or any other that will help my case) to wait until the list is loaded and then use show()
?
function getZvrsti(id){
$.getJSON('test.php?parent='+id, function(data) {
var tmpLi;
$.each(data, function(id, name) {
tmpLi = $('<li><input type="checkbox" value="'+name['id']+'" id="zvrstId'+name['id']+'" /> <label for="zvrstId"'+name['id']+'">'+name['name']+'</label></li>');
$(".drugeZvrsti").append(tmpLi);
tmpLi = "";
});
});
}
This is the function, which uses getJSON
, and here is the snipped where I use show()
:
$(".naprejZvrst").click(function(){
if(!on2){
parent = $(this).attr("id");
getZvrsti(parent);
$(".drugeZvrsti").load(function(){ //my poor example of load()
druga.show(400);
});
on2 = true;
}
else if(on2){
druga.hide(400);
on2 = false;
$(".drugeZvrsti").html('');
}
})
Class drugeZvrsti
is the <ul>
list, where list is shown, var druga
is a <div>
, where the whole <ul>
list is.
NOTE: I've found example of load()
function, but none explained how to use with a list, most were for images.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
$.getJSON
这是$.ajax
默认是异步的,这意味着代码执行将在您完成后继续称之为。您想要做的是在构建并附加列表项后在回调中显示列表。
您还可以注意到我在这里所做的一些其他更改,例如:
.is
检查druga
的可见性,而不是使用变量on2
$.getJSON
which is a shorthand for$.ajax
is per default asynchronous which means that code execution will continue after you've called it.What you want to do is to show the list in the callback once you've built and appended the list items.
You can also note some other changes I've done here, such as:
druga
with.is
instead of using the variableon2
如果您添加一个在 getJSON 成功时执行的回调函数,您可以看到它何时完成。尽管我刚刚让
druga
显示您必须修改它以设置一个变量,然后您将在点击事件中检查该变量。一切都在文档中。If you add a callback function to be executed when
getJSON
has succeeded you can see when it has finished. Although I've just madedruga
show you'll have to modify this to set a variable which you'll then check in your click event. It's all in the docs.你可以使用这样的回调:
...
未经测试的代码
you could use a callback like this:
...
untested code