duanhuo0577 2017-01-04 11:04
浏览 79
已采纳

如何在jquery中获得UL LI值?

I am trying to get ul li listbox value in function but i cant find way.

My code is like that.

<ul class="dropdown-menu" id="newloc">
    <?php
    $res_add = DB::table('res_br')->where('email',$user_email->email)->get();

    if(count($res_add) != "") {
        foreach($res_add as $rr1) { 
        ?> 
            <li value="<?= $rr1->street;?>"><?= $rr1->street;?></li>
        <?php    
        }
    }
    ?>
</ul>

<button type="button" class="btn btn-block btn-primary" data-dismiss="modal" onClick="buildorder()" >Build Your Order</button>

My jquery function is like that

function buildorder() {
    var radio =   $("input[name='newradio']:checked").val();
    var location = $(this).find('li');

    console.log(li.text());
    alert(radio);
    alert(location);
}

can some help me.to get location variable.

  • 写回答

2条回答 默认 最新

  • doumei1203 2017-01-04 11:21
    关注

    Let's fix this code first:

     <ul class="dropdown-menu" id="newloc">
     <?php
         $res_add = DB::table('res_br')->where('email',$user_email->email)->get();
         if(count($res_add) != "")
         {
            foreach($res_add as $rr1)
            {   
                echo '<li value="'.$rr1->street.'">'.$rr1->street.'</li>';
            }
         }
    ?>
    </ul>
    
    <button type="button" class="btn btn-block btn-primary" data-dismiss="modal" onClick="buildorder()" >Build Your Order</button>
    

    Now your code outputs li-elements based upon the foreach.

    Next the button code:

    function buildorder()
    {
    
          var radio =   $("input[name='newradio']:checked").val(); //this refers to an input that is not in this post, but I assume this refers to a location in the list.
          var location = $('#newloc').find('li'); //no this, let's use the id
    
          console.log(location.text()); //this will list all the li elements and there textContent
    
          alert(radio);
          alert(location);
    
    }
    

    This only fixes the code and will give you information from the lis. However since we don't know more about your code I can't help you further with selecting the correct li.

    You can even assign the click handler to the button with jQuery.

    $('button.btn.btn-primary').click(buildorder);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?