循环遍历数组以检查列是否为输入值为空

时间:2014-07-04 11:15:11

标签: javascript jquery arrays

我再次陷入困境,我不知道如何继续,因为我已经对自己的jQuery函数视而不见了。

我想要的是将主表的列保存到数组中,在那里我可以检查哪一个是空的。然后我想将值发送到表。 (第3功能)。

请帮忙!我被困了-_-

JQUERY

//function to save input value below the textbox in a "list" of columns.

$('#SaveButtonId').click(function () {
    $('.table2 tr .table2td').text($('input[type="text"]').val());
    $('input[type="text"]').val('');
});

//function to iterate through .mytd columns and put them in an array.
var columns = [];
$('.table').each(function() {
    $(this).find('.mytd:text').each(function() {
        columns.push($(this).val());
    });
});

//function to send value to empty column
$('#ActivateButton').click(function() {
    $('.table tr .mytd').text($('.table2td').val());
});

HTML

<div id="table-columns">
                <table class="table">
                    <thead>
                        <tr class ="tr-table">
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                        </tr>
                        <tr>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                        </tr>
                        <tr>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                        </tr>

                        <tr>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                        </tr>


                        <tr>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                        </tr>

                        <tr>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                        </tr>
                    </thead>
                </table>
            </div>

     <div class="row">
                    <div class="span2">
                    </div>
                    <div class="span8">
                        <div>
                            <table class="table table-bordered table-condensed" data-bind="visible: items().length > 0">
                                <thead>
                                    <tr>
                                        <th>Items</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr>
                                        <td><input id="Information"type="text"name="Information"/>
                                        </td>
                                        <td>
                                            <input id="SaveButtonId" type="submit" value="Save">
                                        </td>
                                    </tr>
                                </tbody>
                            </table>

                            <table class="table2" style="border: 0;">
                                <thead>
                                <tr>
                                    <td class="table2td" style=" width: 100%"></td>
                                    <td id="Inputs"><input id="ActivateButton"          type="submit" value="Activate &#8679;" /><input id="DeleteButton" type="submit" value="Delete &#10092;" /></td>
                                </tr>
                            </table>

2 个答案:

答案 0 :(得分:1)

这里是代码的fiddle

//function to save input value below the textbox in a "list" of columns.

$('#SaveButtonId').click(function () {
    $('.table2 tr .table2td').text($('input[type="text"]').val());
    $('input[type="text"]').val('');
});

//function to iterate through .mytd columns and put them in an array.
var columns = [];
$('.table').each(function() {
    $(this).find('.mytd:text').each(function() {
        columns.push($(this).val());
    });
});

//function to send value to empty column
$('#ActivateButton').click(function() {
    $('.table tr .mytd').text($('.table2td').val());
});
HTML

<div id="table-columns">
                <table class="table">
                    <thead>
                        <tr class ="tr-table">
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                        </tr>
                        <tr>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                        </tr>
                        <tr>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                        </tr>

                        <tr>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                        </tr>


                        <tr>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                        </tr>

                        <tr>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                            <td class="mytd" style="border-top: groove;"></td>
                        </tr>
                    </thead>
                </table>
            </div>

     <div class="row">
                    <div class="span2">
                    </div>
                    <div class="span8">
                        <div>
                            <table class="table table-bordered table-condensed" data-bind="visible: items().length > 0">
                                <thead>
                                    <tr>
                                        <th>Items</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr>
                                        <td><input id="Information"type="text"name="Information"/>
                                        </td>
                                        <td>
                                            <input id="SaveButtonId" type="submit" value="Save">
                                        </td>
                                    </tr>
                                </tbody>
                            </table>

                            <table class="table2" style="border: 0;">
                                <thead>
                                <tr>
                                    <td class="table2td" style=" width: 100%"></td>
                                    <td id="Inputs"><input id="ActivateButton"          type="submit" value="Activate &#8679;" /><input id="DeleteButton" type="submit" value="Delete &#10092;" /></td>
                                </tr>
                            </table>

答案 1 :(得分:0)

也许你会找这样的东西来循环元素?

$("#table-columns tr td").each(function() { 
   var content = $(this).html(); 
});