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条)

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable