3

我正在尝试使用 JQuery 从动态表中提取数据,但是我尝试过的每一种方式都将值转换为其他值(下面的代码将返回一个“l”而不是 N/AI 所期望的。我尝试过使用 . each 和 .map 作为函数以及 .val .html 和你可以看到的 .text 来提取数据。我不知道为什么以及我做错了什么。任何帮助将不胜感激。

HTML

    <table id="attendees" class="attendees">
    <thead>
        <tr style="border-bottom: double;border-color: #007fff" ;="">
            <th>Full Name</th>
            <th>Membership Type</th>
            <th>Membership Expiration Date</th>
            <th>Free Vouchers</th>
            <th>Classes From Pack Remaining</th>
            <th>Yelp Check in</th>
            <th>Payment Option</th>
            <th>Remove</th>
        </tr>
    </thead>
    <tbody>
        <tr class="data">
            <td>Students Name</td>
            <td>N/A</td>
            <td>N/A</td>
            <td>0</td>
            <td>0</td>
            <td>Yes</td>
            <td>
                <ul id="list">
                    <select id="payment" name="payment">
                        <option>Cash/Credit</option>
                        <option>Free Voucher</option>
                        <option>Yelp Check-in</option>
                    </select>
                </ul>
            </td>
            <td>
                <div><a href="#" class="remove"><p>Remove</p></a>
                </div>
            </td>
        </tr>
    </tbody>
</table>

jQuery

$("#submit").live('click', function (e) {
           $("#attendees tr.data").map(function (index, elem) {
               var x = $(this);
               var cells = x.find('td');
               var $data = cells.text();
               //alert($data[1]);
                if ($data[1] == "N/A") {
                alert(true);    
               }

          });
            e.preventDefault();

        });

最终解决方案

首先感谢所有加入的人,老实说,我从每个人的回答中学到了一些东西。最后,这就是我在下面确定的。事后看来,我可能应该对我试图完成的事情进行更全面的描述。这是扫描多行数据并根据每行中找到的信息做一些事情。为了做到这一点,我被迫将trandtd .each函数分开。这使我可以逐行扫描并仍然获得单个数据。
再次感谢大家的帮助,尤其是@TechHunter

$("#submit").on('click', function (e) {
    e.preventDefault();

    $("#attendees tbody tr").each(function(i) {
    var $data= [];
    var x = $(this);
    var cells = x.find('td');
    $(cells).each(function(i) {
    var $d= $("option:selected", this).val()||$(this).text();
    $data.push($d);
    });      
        if ($data[1] == "N/A") {
           doSomething(); 
        }
    });
});
4

3 回答 3

3

最简单的答案是在需要检索值时添加一个类:

HTML

<table id="attendees" class="attendees">
    <thead>
        <tr style="border-bottom: double;border-color: #007fff" ;="">
            <th>Full Name</th>
            <th>Membership Type</th>
            <th>Membership Expiration Date</th>
            <th>Free Vouchers</th>
            <th>Classes From Pack Remaining</th>
            <th>Yelp Check in</th>
            <th>Payment Option</th>
            <th>Remove</th>
        </tr>
    </thead>
    <tbody>
        <tr class="data">
            <td class="inputValue">Students Name</td>
            <td class="inputValue">N/A</td>
            <td class="inputValue">N/A</td>
            <td class="inputValue">0</td>
            <td class="inputValue">0</td>
            <td class="inputValue">Yes</td>
            <td>
                <ul id="list">
                    <select id="payment" class="inputValue" name="payment">
                        <option>Cash/Credit</option>
                        <option>Free Voucher</option>
                        <option>Yelp Check-in</option>
                    </select>
                </ul>
            </td>
            <td>
                <div><a href="#" class="remove"><p>Remove</p></a>
                </div>
            </td>
        </tr>
    </tbody>
</table>

Javascript

$("#submit").on('click', function (e) {
    e.preventDefault();
    var data = $("#attendees tr.data").map(function (index, elem) {
        var ret = [];
        $('.inputValue', this).each(function () {
            var d = $(this).val()||$(this).text();
            ret.push(d);
            console.log(d);
            if (d == "N/A") {
                console.log(true);
            }
        });
        return ret;
    });
    console.log(data);
    console.log(data[0]);
});

它更容易,您只检索您想要检索的值。快速实施和维护。

在这里提琴

于 2013-05-29T07:07:23.410 回答
1

您应该使用on()代替live() (如果您的 JQuery 版本支持它) REASON。只是像这样修改了你的jquery并且它起作用了

jQuery-

$("#submit").on('click', function (e) {

       $("#attendees tr.data").map(function (index, elem) {
           $(this).find('td').each(function(){
           var $data = $(this).html();
           alert($data);
            if ($data == "N/A") {
            alert(true);    
           }
        });
      });
        e.preventDefault();

    });

更新:-

只需检查td. 如果是,那么你可以得到它,如下所示

if($(this).find("select").length > 0)
     {
         alert("found select")
         alert($(this).find("select").val())
     }

此处演示:- FIDDLE(更新后的显示选择框中选择的

于 2013-05-29T04:17:06.867 回答
0

有几件事,假设您使用的是最新版本的 jQuery,您将要替换live()on().

此外,您需要遍历您的单元格以找到您要查找的内容,只需将其放在变量中将无法解决问题。

$(document).on('click', '#submit' function (e) {
    e.preventDefault();

    //time to iterate over the cells
    $("#attendees tr.data td").each(function(i) {
        if ($(this).text() == "N/A") {
            alert("Found it on cell index: " +i);
        }
    });
});

如果要使用.map()返回文本数组td,可以执行以下操作:

var tdArray = $("#attendees tr.data td").map(function() {
    return $(this).text();
}).get();
于 2013-05-29T04:06:31.777 回答