【问题标题】:Send ID of list-menu to ajax get function in CodeIgniter将列表菜单的 ID 发送到 CodeIgniter 中的 ajax get 函数
【发布时间】:2015-03-09 07:49:53
【问题描述】:

我想在不刷新页面的情况下加载 div 记录。

为此,我创建了:

$('.list-group-item').click(function(e) {
var id = $(this).attr('id');

    $.ajax({
        type: 'get',
        url: 'http://localhost/ajax/main/'+id,
        dataType: 'html',
        success: function (html) {
             $('#yourDiv').html(html);
        }
    });
});

还有菜单:

            <ul class="list-group sidebar-nav-v1 fa-fixed" id="sidebar-nav">
                <li class="list-group-item list-toggle">                   
                    <a data-toggle="collapse" data-parent="#sidebar-nav" href="#collapse-typography">Menu</a>
                    <ul id="collapse-typography" class="collapse in">
                        <li><a href="#1" id="test"><i class="fa fa-sort-alpha-asc"></i> Test 1 </a></li>
                        <li><a href="#2" id="test1"><i class="fa fa-magic"></i> Test 2 </a></li>
                        <li><a href="#3" id="test2"><i class="fa fa-ellipsis-h"></i> Test 3 </a></li>
                        <li><a href="#4" id="test3"><i class="fa fa-quote-left"></i> Test 4 </a></li>
                    </ul>
                </li>
            </ul>

获取菜单项href id的正确方法是什么?

【问题讨论】:

    标签: jquery ajax codeigniter


    【解决方案1】:

    使用以下代码获取 href(a) 标签的点击事件。然后你会在 "$(this).attr('id')" 中得到 id

    $('.list-group-item').find('a').click(function(e) {
      var id = $(this).attr('id');
      $.ajax({
        type: 'get',
        url: 'http://localhost/ajax/main/'+id,
        dataType: 'html',
        success: function (html) {
             $('#yourDiv').html(html);
        }
       });
     });
    

    【讨论】:

    • 感谢您的快速回放。
    【解决方案2】:

    改变

    $('.list-group-item').click(function(e) {
    

    $('#collapse-typography a').click(function(e) {
    

    Demo

    【讨论】:

    • 欢迎@George 很高兴为您提供帮助。
    • 我有一个修改后的问题。有没有办法记住 id 以便当我使用 F5 时刷新它所在的页面?
    猜你喜欢
    • 1970-01-01
    • 2018-09-24
    • 1970-01-01
    • 2018-06-01
    • 1970-01-01
    • 2021-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多