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

如何在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条)

相关推荐 更多相似问题