douxiong0668 2017-05-04 08:45 采纳率: 100%
浏览 121
已采纳

获取li值并POST到ajax

I have a treeview list, and I want to send the id of li select with ajax but I don't know why this code doesn't works

The code: http://jsfiddle.net/esS4k/379/ (old)

The new code(http://jsfiddle.net/esS4k/385/), try it, the alert on li value is "undefined"

-HTML

<div id="content_table">
  <ul id="treeview">
  <li data-expanded="true">Item 1
      <ul>
          <li id="11">Item 1.1
            <ul>
              <li id="111">Item1.1.1</li>
              <li id="112">Item1.1.2</li>
            </ul>
           </li>
          <li id="12">Item 1.2</li>
      </ul>
  </li>
  <li data-expanded="true">Item 2
      <ul>
          <li id="21">Item 2.1</li>
          <li id="22">Item 2.2</li>
      </ul>
  </li>
 </ul>
</div>

-jQuery/ajax

$(document).ready(function () {
$('#treeview').change(function () {
    $.ajax({
        url: "ldtree2.php",
        method: "POST",
        data: 'choice' + $("ul[name='treeview']>option:li.selected").val(),
        dataType: "html",
        success: function (data) {
        $('#content_table').html(data);                                 
        }
    });
}); 
});

-ldtree2.php

<?php echo $_POST['choice'];?>

Have a good day

EDIT: I think, the problem is because of the list structure

**<li>** <ul><li></li><li></li></ul> **<li>** 

With the event

    $('#treeview li').on('click',function(){         
        alert( this.id);         
})

I have 2 alerts (1 with the "li" id and 1 with nothing)

  • 写回答

5条回答 默认 最新

  • doulei2100 2017-05-04 09:17
    关注

    The following code will do the work for you. You just need to add test to all the child ul

    var data={ choice:$(this).attr('id') };
    $('#treeview .test li').on('click',function(){          
            $.ajax({
            url: "ldtree2.php",
            method: "POST",
            data: data,
            dataType: "html",
            success: function (data) {
            $('#content_table').html(data);                                 
            }
        });            
    });
    

    NOTE : Tested the same in your fiddel, please check http://jsfiddle.net/esS4k/380/

    Hope that helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码