Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery-加载后显示列表_Jquery_Json_Load_Show - Fatal编程技术网

jQuery-加载后显示列表

jQuery-加载后显示列表,jquery,json,load,show,Jquery,Json,Load,Show,我制作了一个脚本,使用getJSON()从MySQL数据库的“父”类型中获取子类型。为了显示子类型的列表,我使用了show()函数,但是因为列表还没有加载,它会弹出,我不喜欢这样。如何使用load()函数(或任何其他有助于我的案例)等待列表加载,然后使用show() ClassdrugeZvrsti是列表,其中显示列表,var-druga是,其中显示整个列表。 注意:我找到了load()函数的示例,但没有一个解释如何与列表一起使用,大多数用于图像。如果您添加了一个回调函数以在getJSON成功时

我制作了一个脚本,使用
getJSON()
从MySQL数据库的“父”类型中获取子类型。为了显示子类型的列表,我使用了
show()
函数,但是因为列表还没有加载,它会弹出,我不喜欢这样。如何使用
load()
函数(或任何其他有助于我的案例)等待列表加载,然后使用
show()

Class
drugeZvrsti
    列表,其中显示列表,
    var-druga
    ,其中显示整个
      列表。
      
      注意:我找到了
      load()
      函数的示例,但没有一个解释如何与列表一起使用,大多数用于图像。

      如果您添加了一个回调函数以在
      getJSON
      成功时执行,您可以看到它何时完成。虽然我刚刚演示了
      druga
      ,但您必须修改此设置以设置一个变量,然后在单击事件中检查该变量。一切都在计划之中

      函数成功函数(数据){
      druga.show(400);
      }
      函数getZvrsti(id){
      $.getJSON('test.php?parent='+id,函数(数据){
      var-tmpLi;
      $.each(数据、函数(id、名称){
      tmpLi=$('
    • 是is per default asynchronous的缩写,这意味着调用后代码将继续执行

      您要做的是在构建并附加列表项后在回调中显示列表

      // Let's define our local scope variables
      var druga = $(".drugeZvrsti"),
          request = false;
      
      function getZvrsti(id) {
          // Save the request to our requests object
          requests[id] = $.getJSON('test.php?parent='+id, function(data) {
              var html = "";
              $.each(data, function(id, name) {
                  html += '<li><input type="checkbox" value="'+name['id']+'" id="zvrstId'+name['id']+'" /> <label for="zvrstId"'+name['id']+'">'+name['name']+'</label></li>';
              });
              // Append the list items and then fade in
              druga.append(html).show(400);
              // We no longer have a request going
              request = false;
          });
      }
      
      $(".naprejZvrst").click(function(){
          var id = $(this).attr("id");
          // Since we don't want multiple requests, abort any existing one
          if (request) {
              request.abort();
              request = false;
          }
          if (druga.is(":hidden")) {
              getZvrsti(id);
          } else {
              // Stop any animations, fade out and
              // use callback to empty the list when its hidden
              druga.stop().hide(400, function(){
                  druga.empty();
              });
          }
      });
      
      //让我们定义局部范围变量
      var druga=$(“.drugeZvrsti”),
      请求=假;
      函数getZvrsti(id){
      //将请求保存到我们的请求对象
      请求[id]=$.getJSON('test.php?parent='+id,函数(数据){
      var html=“”;
      $.each(数据、函数(id、名称){
      
      html+='
    • 您可以使用如下回调:

      function getZvrsti(id, callback){
          $.getJSON('test.php?parent='+id, callback(data));
      }
      

      getZvrsti(父级,函数(数据){
      var-tmpLi;
      $.each(数据、函数(id、名称){
      
      tmpLi=$(“
    • @MarcusEkwall-我没有测试它,只是让它指向正确的方向。你有没有任何关于为什么这不起作用的反馈?你更新了你的代码,但它仍然是错误的。
      $。每个
      都不接受回调。此外,你不是作为参数传递它,而是它的实际结果。正如你所看到的,已经有一个callb确认,因此确实不需要添加第二个,如果确实需要,正确的方法是向
      $.getJSON
      调用添加
      .done(successFunction)
      。好的,就像我说的,我还没有实际测试过它-我看不到有提到
      .done()
      在文档中。@medmondson通过
      $返回的对象。getJSON
      实现了a的行为。我建议使用自
      。成功将是。很棒的脚本!现在几乎所有东西都可以正常工作!现在只有一个问题:如果我显示druga,隐藏它并再次尝试显示它,则不会出现任何令人烦恼的东西。无需担心那个,我的剧本有点不同,现在都修好了。谢谢!
      
      function successFunction(data) {
        druga.show(400);
      }
      
      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 = "";
                  });
              }, successFunction(i));
          }
      
      // Let's define our local scope variables
      var druga = $(".drugeZvrsti"),
          request = false;
      
      function getZvrsti(id) {
          // Save the request to our requests object
          requests[id] = $.getJSON('test.php?parent='+id, function(data) {
              var html = "";
              $.each(data, function(id, name) {
                  html += '<li><input type="checkbox" value="'+name['id']+'" id="zvrstId'+name['id']+'" /> <label for="zvrstId"'+name['id']+'">'+name['name']+'</label></li>';
              });
              // Append the list items and then fade in
              druga.append(html).show(400);
              // We no longer have a request going
              request = false;
          });
      }
      
      $(".naprejZvrst").click(function(){
          var id = $(this).attr("id");
          // Since we don't want multiple requests, abort any existing one
          if (request) {
              request.abort();
              request = false;
          }
          if (druga.is(":hidden")) {
              getZvrsti(id);
          } else {
              // Stop any animations, fade out and
              // use callback to empty the list when its hidden
              druga.stop().hide(400, function(){
                  druga.empty();
              });
          }
      });
      
      function getZvrsti(id, callback){
          $.getJSON('test.php?parent='+id, callback(data));
      }
      
       getZvrsti(parent, 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 = "";
              });
      
             druga.show(400);
       });