简体   繁体   English

从JavaScript调用jQuery函数

[英]Calling a jQuery function from JavaScript

I am trying to import a .csv file in my JavaScript and trying to save it in an array. 我正在尝试在JavaScript中导入.csv文件,并尝试将其保存在数组中。 I got the below sample while searching on stackoverflow.com to use jQuery-CSV library's function called csv2Dictionary() to do so. 在stackoverflow.com上搜索以使用jQuery-CSV库的名为csv2Dictionary()的函数时,得到了以下示例。

var data = $.csv2Dictionary(csv):

In above line of code "data" will give me my array, but can some one help me know before I get to this step, how can I call csv2Dictionary() from my JavaScript? 在上面的代码行中,“数据”将给我我的数组,但是在进行此步骤之前,可以帮助我知道如何在JavaScript中调用csv2Dictionary()

What do you exactly mean "call it from my javascript"? 您确切的意思是“使用我的JavaScript调用它”?

The answer is easy: do it ;) 答案很容易: 做到 ;)

Just make sure that jQuery and jQuery-CSV are loaded before you try to call the function. 在尝试调用该函数之前,只需确保已加载jQuery和jQuery-CSV。 Then you can easily do something like this: 然后,您可以轻松地执行以下操作:

var csv;
// ...
// Create your CSV
// ...
$.csv2Dictionary(csv);
    readTheFileWhenLoaded = function(){
        $('#inputTypeFileInHTML').change(function(event){
            readTheFile(this.files[0]);
            $('#inputTypeFileInHTML').val(null);
        });
    }

     readTheFile= function(file){
        var fileContent;
          var r = new FileReader();
          r.onload = function(e) {
              fileContent= e.target.result;
              fileContentAsStringJavaScriptVar(fileContent);
          }
          r.readAsText(file);
    }

    fileContentAsStringJavaScriptVar= function(fileContent){
        console.log(fileContent);
         $.csv2Dictionary(fileContent);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM