如何在jquery中获得UL LI值?

时间:2017-01-04 11:04:51

标签: javascript php jquery

我试图在功能中获取ul li列表框值,但我找不到。

我的代码就是这样。

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

我的jquery函数就是那样

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

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

可以帮助我获取位置变量。

2 个答案:

答案 0 :(得分:0)

使用此代码,希望这会有所帮助

$("#newloc li").click(function() {
alert(this.id); 
alert($(this).html()); 
alert($(this).text()); 

});

答案 1 :(得分:0)

让我们先修复此代码:

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

现在,您的代码会输出li - 基于foreach的元素。

接下来是按钮代码:

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

}

这只会修复代码,并会为您提供li的信息。但是,由于我们对您的代码了解不多,因此我无法帮助您进一步选择正确的li

您甚至可以使用jQuery将点击处理程序分配给按钮。

$('button.btn.btn-primary').click(buildorder);