如何提取使用JQuery嵌套的HTML内的文本? [英] How to extract text inside nested HTML using JQuery?

查看:128
本文介绍了如何提取使用JQuery嵌套的HTML内的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里有HTML code:

I have here HTML Code:

<div class="actResult" style="border: solid">
    <table>
        <tbody>
            <tr>
                <td>Order Number</td>
                <td>1</td>
            </tr>
            <tr>
                <td>Customer Number</td>
                <td>3</td>
            </tr>
            <tr>
                <td>Complaint Code</td>
                <td>b</td>
            </tr>
            <tr>
                <td>Receivable Receipt Number</td>
                <td>5</td>
            </tr>
            <tr>
                <td>Date Called</td>
                <td>2014-03-19</td>
            </tr>
            <tr>
                <td>Scheduled Day Of Checkup</td>
                <td>2014-03-19</td>
            </tr>
            <tr>
                <td>Scheduled Day Of Service</td>
                <td>2014-03-21</td>
            </tr>
            <tr>
                <td>Checkup Status</td>
                <td>Y</td>
            </tr>
            <tr>
                <td>Service Status</td>
                <td>N</td>
            </tr>
            <tr>
                <td>Technician Number Checkup</td>
                <td>3</td>
            </tr>
            <tr>
                <td>Technician Number Service</td>
                <td>1</td>
            </tr>
        </tbody>
    </table>
</div>

我想要得到的变量的值,并把它们放到一个数组像阵列(首个TD=>第二个TD)的结构,所以这种情况下,阵列将阵列(订单号=>1,客户编号=>3,投诉code=>b,......)等等。

I want to get the values of the tags and put them into an array with the a structure like array("first td" => "second td"), so for this case the array would be array("Order Number" => "1", "Customer Number" => "3", "Complaint Code" => "b", ...) and so on.

在此之后,最终阵列将被发送到一个PHP code

After that, the final array would be sent into a PHP code.

我一直在试图提取一些使用 VAR的html = $(本).filter HTML中的值(函数(指数){返回$(TD,这一点)}) .filter(:奇)。文本(); 和过滤器()的各种其他组合,但它似乎并没有为我工作

I've been trying to extract some of the values from the HTML using var html = $(this).filter(function( index ){ return $("td", this) }).filter(":odd").text(); and various other combinations of filter(), but it doesn't seem to work for me.

我如何去做我想做的事?

How do I go about doing what I want to do?

推荐答案

的jsfiddle演示

jsFiddle Demo

您会希望使用。每个为,并通过在表中的行进行迭代。对于每一行,采取的第一个单元格( .EQ(0))为重点,第二个单元格( .EQ(1))的值。将这些在结果对象。

You are going to want to use .each for that and iterate through the rows in the table. For each row, take the first cell (.eq(0)) as the key, and the second cell (.eq(1)) as the value. Place these in a result object.

//object to hold resulting data
var result = {};

//iterate through rows
$('.actResult tr').each(function(){

 //get collection of cells
 var $tds = $(this).find('td');

 //set the key in result to the first cell, and the value to the second cell
 result[$tds.eq(0).html()] = $tds.eq(1).text();
});

这篇关于如何提取使用JQuery嵌套的HTML内的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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