繁体   English   中英

如何在jQuery中获取UL LI值?

[英]how to get UL LI value in jquery?

我试图在功能中获取ul li listbox值,但我找不到方法。

我的代码就是这样。

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

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

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

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

});

让我们先修复此代码:

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

现在,您的代码基于foreach输出li elements。

接下来的按钮代码:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM