使用jquery和php

时间:2019-01-16 08:35:02

标签: php jquery codeigniter

正在处理需要动态生成侧边栏菜单的项目。

<div class="sidebar-menu">
    <ul>
      <li class="header-menu">
        <span>General</span>
      </li>
      <?php                 
            foreach ($Business_Types as $key => $business) 
            { ?>

      <li class="">
        <a href="#">
          <span id="<?php echo $business->business_id ?>"><?php echo $business->business_name; ?></span>
          <span class="badge badge-pill badge-warning">New</span>
        </a>
            <?php } ?>
      </li>
    </ul>
  </div>

这是jquery代码:

$('#menu_id or class_name ').click(function(){
    var obj = $('menu_id').val();
    if(obj != ''){
        $.ajax({
            url:"<?php echo base_url();?>Vendors_search/check_obj_availability",
            method:"POST",
            data:{obj:obj},
            dataType: "json",
            success:function(resp){
                if(resp.status == "success")
                $('#search_x_result').html('<div class="alert alert-success">' + resp.message + '</div>');

                else    
                $('#search_x_result').html('<div class="alert alert-danger">' + resp.message + '</div>');

            }
        });
        return false;
    }
});

因此,任何人都可以帮助我为使用jquery提取的每个菜单提供ID或类。

2 个答案:

答案 0 :(得分:2)

我先给ID命名,然后再加上ID。像下面一样

<span id="menuitem_<?php echo $business->business_id ?>"><?php echo $business->business_name; ?></span>

如果您有5件物品,它们会掉出来,但是像:

<span id="menuitem_1">Test Business</span>
<span id="menuitem_2">Test Business2</span>
<span id="menuitem_3">Test Business3</span>
<span id="menuitem_4">Test Business4</span>
<span id="menuitem_5">Test Business5</span>

然后使用ajax和jquery获取单击的菜单项ID。

<script type="text/javascript">

$('[id^="menuitem_"]').on('click',function(){

    var menuID = $(this).attr('id').split("_")[1]; // this will give you the id of the clicked item


    //continue with ajax to fetch data for the item


});
</script>

答案 1 :(得分:0)

$('.sidebar-menu').attr('id', 'value');

您可以使用javascript中选择的动态创建/获取的值替换“ value”