如何获取此 ul 列表的值
代码是这样的:
<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>
我尝试了以下代码来访问该值:
$("#eventselect").val();
$("#eventselect :selected").text();
但没有任何效果,现在我的问题是如何获取该值。
编辑 - 1
大家好, 抱歉,我意识到我不应该使用这个 $("#eventselect :selected").text(); 。有一个问题,我得到的列表以空选择框开头,我认为它来自
,如果我删除它,我我没有在列表中得到任何东西。通过使用该标签,我可以使用 $("#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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
访问 UL 的“选定值”的一种方法是,如果您可以通过类将其中一个与其他值区分开来。假设选定的元素在 LI 元素上具有
selected
类。然后您可以像这样访问它:或者如果您想要
rel
属性:将所选值与其他属性区分开来的一种方法是这样做:
您可以通过单击获取所选值事件也一样,像这样:
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:or maybe if you want the
rel
attribute:Where one way of distinct the selected one from the others it's doing something like this:
You can get the selected value with the click event too, like this: