如何通过UL内部的LI创建值数组 [英] How to create an array of values from LI inside a UL

查看:97
本文介绍了如何通过UL内部的LI创建值数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控件,允许用户以所需的顺序对<li>进行排序.当表单提交时,我想按表单上的顺序在每个放入数组的<li>中抓取<li>内的文本.

I have a control which allows users to sort the <li> in whatever order they want. when the form submits I want to grab the text inside the <li> for each <li> put into an array, in the order on the form.

<ul id="sortable">
    <li class="ui-state-default">
        <span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Protective Services
    </li>
    <li class="ui-state-default">
        <span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Engineering Services and Public Works
    </li>
</ul>

我正在抓取<li>的是:

var ar = [];
ar = document.getElementById("sortable").getElementsByTagName("li");

然后我遍历数组:

for(i = 0; i < ar.length; i++){
    alert(ar[i].text());    //ar[i].anything results in console errors.
}

ar [i]对于每个可用的<li>显示[object HTMLLIElement]. 如果我尝试访问项目内的.text/.val/id属性,则会收到控制台错误.所以我假设这与解析/转换问题有关?

ar[i] displays [object HTMLLIElement] for every <li> available. if I try to access the .text/.val/id properties inside the items i get a console error. So I'm assuming this has to do with a parsing/conversion issue?

如何正确创建看起来像protective services,Engineering Services and Public Works而不是[object HTMLLIElement],[object HTMLLIElement]的数组?或者如何以[object HTMLLIElement]身份从<li>访问我的文本信息?

How do I properly create an array that looks like protective services,Engineering Services and Public Works and NOT like [object HTMLLIElement],[object HTMLLIElement]? Or how do I access my text information from the <li> as a [object HTMLLIElement]?

推荐答案

您需要获取jQuery对象才能使用text():

You need to get the jQuery object in order to use text():

alert($(ar[i]).text());

这篇关于如何通过UL内部的LI创建值数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆