使用Jquery

时间:2016-03-02 10:29:23

标签: javascript jquery file csv

我需要从本地服务器读取CSV文件的内容。在我的CSV文件中,我有三个字段,分别是:

  • 区域,
  • Acct_Name,
  • 状态。

获取Acct_Name值中的值时采用逗号格式。在我的CSV文件阅读器中,它将分隔逗号并且该值将进入状态。

那么我该如何解决这个问题?

我的代码是:

$(document).ready(function() {
 // AJAX in the data file
    $.ajax({
        type: "GET",
        url: "data.csv",
        dataType: "text",
        success: function(data) {processData(data);}
        });

    // Let's process the data from the data file
    function processData(data) {
        var table = $("<table />");
                    var rows = data.split(/\r\n|\n/);
                    for (var i = 1; i < rows.length-1; i++) {
                        var row = $("<tr />");
                        var cells = rows[i].split(",");

                        for (var j = 0; j < rows.length; j++) {
                            var cell = $("<td />");
                            cell.html(cells[j]);
                            row.append(cell);
                        }

                          var usedNames = {};
                            $("select[name='company1'] > option").each(function () {
                                if(usedNames[this.text]) {

                                    $(this).remove();
                                } else {
                                    usedNames[this.text] = this.value;
                                }
                            });
                              $("select[name='company2'] > option").each(function () {
                                if(usedNames[this.text]) {

                                    $(this).remove();
                                } else {
                                    usedNames[this.text] = this.value;
                                }
                            });
                                $("select[name='company3'] > option").each(function () {
                                if(usedNames[this.text]) {

                                    $(this).remove();
                                } else {
                                    usedNames[this.text] = this.value;
                                }
                            });
                            $("#region").append("<option value =1> " + cells[0] + " </option>");
                            $("#state").append("<option value =1> " + cells[1] + "</option>");
                            $("#accname").append("<option value =1>" + cells[2] + "</option>");

                        table.append(row);
                    }

    }
});

0 个答案:

没有答案
相关问题