2

我正在寻找一种将 的数据响应附加到调用它$.post()的高度的方法。li

我知道它已经完成,但我找不到任何可以阅读的内容。那么,有什么想法吗?

方案

  • 列表项1

  • 列表项2

  • 清单 item3,点击这里:

                          * sublist item1
                          * sublist item2
                          * sublist item3
    
  • 清单项目4

更新

我拥有的唯一代码是有效的$.post(),我console.log(data)只是为了查看响应。

好的,我想实现这样的目标:http ://www.alistapart.com/d/horizdropdowns/horizo​​ntal.htm的$.post()响应

4

3 回答 3

1

关于它的外观的想法......

 $('yourliclass').click(function(){
       //do ajax
       $.ajax({
        .......
        .......

        success:function(data){ //data needs to be in json format
           var htmlcontent = "";
        htmlcontent = '<ul>';
        for(var i=0;i<data.length;i++){
        htmlcontent += '<li>'+data[i]['your_index']+'</li>';
    }
    htmlcontent += '</ul>';
        }
$(this).appendTo(htmlcontent);


});

}

于 2012-05-16T09:25:32.770 回答
1

试试这个

        var data;
        $(document).ready(function()
        {

            $(".list li").click(function()
            {
                $.post("filename.php",function(res)
                {
                     data="<li>"+res+"</li>";           
                });
                $(this).after(data);
            });
        });     

<body>
    <ul class="list">
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
        <li>Item 4</li>
    </ul>
</body> 
于 2012-05-16T09:26:10.780 回答
1
$('li.clickable').on('click', function() {
  var list = $(this);
  $.ajax({
      url: 'url_to_script',
      method: 'post',
      success: function(res) {

         var response = $(res); // assume that you response contains li tags i.e '<li>subitem 1</li>' and more
         list.append(response); // append response to list that clicked

         /* >> OR  << */
         // if you response contains data as array/field
         $.each(res, function(index, data) {
            list.append('<li>'+ data['some_text'] +'</li>'); // some_text is assumed property, you have something else
         });

        // ACCORDING TO YOUR COMMENT
        var sublist = '<ul>';
        $.each(res, function(index, data) {
            sublist += '<li>'+ data['some_text'] +'</li>'; // some_text is assumed property, you have something else
         });
            sublist += '</ul>'; // now sublist is like <ul><li>...</li>....</ul>
        list.append(sublist); // append the sublist to ul.clickable
      }
  });
});

注意:您必须制作一些 CSS 来改善外观

于 2012-05-16T09:27:06.270 回答