如何获取此 ul 列表的值

发布于 2024-12-11 13:45:36 字数 1553 浏览 2 评论 0原文

代码是这样的:

<ins>
    <div class="dropdown">
        <ins>&nbsp;</ins>
        <ul id="eventselect">
            <li><a href="javascript:;" rel="Birthdays">Birthdays</a></li>
            <li><a href="javascript:;" rel="Graduation">Graduation</a></li>
            <li><a href="javascript:;" rel="Wedding">Wedding</a></li>
            <li><a href="javascript:;" rel="Farewell">Farewell</a></li>
            <li><a href="javascript:;" rel="Party">Party</a></li>
            <li><a href="javascript:;" rel="Bbq">Bbq</a></li>
            <li><a href="javascript:;" rel="Party">Party</a></li>
            <li><a href="javascript:;" rel="Warming_party">Housewarming</a></li>
            <li><a href="javascript:;" rel="Christmas">Christmas</a></li>
            <li><a href="javascript:;" rel="Other">Other</a></li>
        </ul>
    </div>
</ins>

我尝试了以下代码来访问该值:

$("#eventselect").val();
$("#eventselect :selected").text();

但没有任何效果,现在我的问题是如何获取该值。

编辑 - 1

大家好, 抱歉,我意识到我不应该使用这个 $("#eventselect :selected").text(); 。有一个问题,我得到的列表以空选择框开头,我认为它来自 &nbsp;,如果我删除它,我我没有在列表中得到任何东西。通过使用该标签,我可以使用 $("#eventselect li").text(); 访问值或文本。但即使第一个 li 为空白,该值也是生日。如何纠正呢?

The code is this:

<ins>
    <div class="dropdown">
        <ins> </ins>
        <ul id="eventselect">
            <li><a href="javascript:;" rel="Birthdays">Birthdays</a></li>
            <li><a href="javascript:;" rel="Graduation">Graduation</a></li>
            <li><a href="javascript:;" rel="Wedding">Wedding</a></li>
            <li><a href="javascript:;" rel="Farewell">Farewell</a></li>
            <li><a href="javascript:;" rel="Party">Party</a></li>
            <li><a href="javascript:;" rel="Bbq">Bbq</a></li>
            <li><a href="javascript:;" rel="Party">Party</a></li>
            <li><a href="javascript:;" rel="Warming_party">Housewarming</a></li>
            <li><a href="javascript:;" rel="Christmas">Christmas</a></li>
            <li><a href="javascript:;" rel="Other">Other</a></li>
        </ul>
    </div>
</ins>

I have tried following code for to access the value:

$("#eventselect").val();
$("#eventselect :selected").text();

But nothing worked, now my question is how to get the value of this please.

EDIT - 1

Hi guys,
sorry, I realized that i shouldnt have used this $("#eventselect :selected").text(); . There is a problem which I am getting the list starts with an empty select box, which I think comes from that <ins> </ins>, and if I remove that I am not getting anything in the list. And with that tag inside I can access the value or text by using $("#eventselect li").text();. But the value is birthdays even when the blank first li is there. How to rectify it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你在看孤独的风景 2024-12-18 13:45:36

访问 UL 的“选定值”的一种方法是,如果您可以通过类将其中一个与其他值区分开来。假设选定的元素在 LI 元素上具有 selected 类。然后您可以像这样访问它:

$('#eventselect li.selected a').text();

或者如果您想要 rel 属性:

$('#eventselect li.selected a').attr('rel');

将所选值与其他属性区分开来的一种方法是这样做:

$('#eventselect li a').click(function(){
    $('#eventselect li').removeClass('selected'); // remove all selected class values
    $(this).parent().addClass('selected'); // add class selected to selected value
});

您可以通过单击获取所选值事件也一样,像这样:

$('#eventselect li a').click(function(){
    $(this).attr('ref'); // Get the ref attribute from the "a" element
});

A way of accessing the "selected value" of an UL its if you can distinct one from the others, maybe with a class. Let's say the selected one get to have the selected class on the LI element. Then you could access it like this:

$('#eventselect li.selected a').text();

or maybe if you want the rel attribute:

$('#eventselect li.selected a').attr('rel');

Where one way of distinct the selected one from the others it's doing something like this:

$('#eventselect li a').click(function(){
    $('#eventselect li').removeClass('selected'); // remove all selected class values
    $(this).parent().addClass('selected'); // add class selected to selected value
});

You can get the selected value with the click event too, like this:

$('#eventselect li a').click(function(){
    $(this).attr('ref'); // Get the ref attribute from the "a" element
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文