如何通过JQuery中的每个循环获取数组中提取的数据

时间:2017-10-12 12:12:55

标签: jquery

这是我的HTML代码:

<form onSubmit="return false;" id="patientPastHistory">
    <div class="row">
        <div class="col-md-11">
            <div class="portlet box blue-hoki">
                <div class="portlet-title">
                    <div class="caption">
                        Past History
                    </div>
                </div>
                <div class="portlet-body">
                    <div class="row">
                        <div class="col-md-12">
                            <div class="row">
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <input type="text" id="medicalCondition" class="form-control typeahead-findings circle-input" style="text-transform: capitalize;" placeholder="Medical Condition">
                                            <label for="medicalCondition">E.g. Asthama, Arthiritis, etc.</label>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <select class="form-control" id="medicalStatus">
                                        <option selected disabled value="">Select Treatment Status</option>
                                        <option value="Treated">Treated</option>
                                        <option value="Ongoing">Ongoing Treatment</option>
                                    </select>
                                </div>
                                <div class="col-md-1 col-sm-6 col-xs-6">
                                    <a id="btnAdd" class="btn green-meadow mt-repeater-btn-inline btn-circle"><i class="fa fa-plus"></i></a>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <table id="medicalCondition" class="table table-bordered table-striped">
                                <thead>
                                    <tr>
                                        <th width="60%"> Medical Condition </th>
                                        <th width="30%"> Status </th>
                                        <th width="10%"> Action </th>
                                    </tr>
                                </thead>
                                <tbody id="treatmentTbl">
                                </tbody>
                            </table>
                        </div>
                    </div>
                    <div class="row">
                        {{-- Submit Button --}}
                        <div class="col-md-8 col-xs-12 col-sm-12 col-md-offset-2  mt-35">
                            <div class="col-md-6 col-sm-6 col-xs-6 nopad">
                                <button id="btnpatientPastHistory" class="fwb btn btn-circle green-meadow">
                                <i class="fa fa-check"></i> Save Changes</button>
                            </div>
                            <div class="col-md-6 col-sm-6 col-xs-6 nopad">
                                <button id="cancel" type="button" class="fwb btn btn-circle btn-outline dark"><i class="fa fa-close"></i>Cancel</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>

在此表中,我附加了通过JQuery从输入文件中获取的数据,这是代码:

$('#btnAdd').click(function()
    {
        var count = $("#treatmentTbl tr").length + 1;
        var medicalCondition = $("#medicalCondition").val();
        var medicalStatus = $("#medicalStatus").val();

        if (medicalCondition != "")
            {
                if (medicalStatus == "Treated")
                    {
                        $('#treatmentTbl').append("<tr style='text-transform: capitalize;' class='treatmentRow' data-id="+count+"><td data-name="+medicalCondition+">"+medicalCondition+"</td><td><span class='label label-success' data-name="+medicalStatus+">"+medicalStatus+"</span></td><td><a class='delBtn btn-circle btn btn-danger'><i class='fa fa-minus'></i></a></td></tr>");
                    }
                else
                    {
                        $('#treatmentTbl').append("<tr style='text-transform: capitalize;' class='treatmentRow' data-id="+count+"><td data-name="+medicalCondition+">"+medicalCondition+"</td><td><span class='label label-danger' data-name="+medicalStatus+">"+medicalStatus+"</span></td><td><a class='delBtn btn-circle btn btn-danger'><i class='fa fa-minus'></i></a></td></tr>");
                    }

                $("#medicalCondition").val('');
                $("#medicalStatus").prop("selectedIndex", 0);
            }
    });

这是我的自定义JQuery代码,用于获取表中反映的数据以发送到服务器:

$("button#btnpatientPastHistory").click(function()
    {
        var data = [];
        var dataAttributes = ["medicalCondition: ", "treatmentStatus:"];

        $('table#medicalCondition > tbody#treatmentTbl  > tr > td').each(function(i)
            {
                data.push(dataAttributes[i]+$(this).data("name"));

                if(i % 2 == 0)
                    {
                        data.push(dataAttributes[i]+$(this > 'span').data("name"));
                    }
            });

        console.log(data);
    });

我想要做的是我试图从表中获取数据并将其转储到数组变量意味着像dataArray = []这样的数组数据然后我想在此存储数据,如果存储表中的第一个数据然后应该在dataArray中使用[1]创建一个索引,然后将第一个tr行数据存储在其中,依此类推,直到数据完成。

但不知怎的,我无法做到这一点到目前为止我已经在上面发布的JQuery代码中使用了我的逻辑但是我不知道大多数时候它在NAN的控制台中给出了错误。任何人都可以帮我解释这个逻辑吗?

0 个答案:

没有答案