2

ここで私が使用しているもの

$(document).ready(function(){
  $("#button").click(function(){
    $("#box").load("read.txt"); // i want read.txt content in variable
  });
});

このコードは正常に動作していますが、変数の内容が必要です。

4

2 に答える 2

2

これを使うだけ

function validate(content) {
    alert(content); // check your content here
}
$(document).ready(function(){
  $("#button").click(function(){
    //$("#box").load("read.txt"); // skip this line
    $.get('read.txt',validate); // new line
  });
});
于 2013-06-05T10:15:01.523 に答える
1

The jQuery $.load method loads a url, and outputs it's contents into a jQuery DOM element. It is a shorthand for the $.ajax method, but preconfigured to write the result into an element.

You can use the original $.ajax method or one of it's other shorthands to better suit your needs.

jQuery Ajax Shorthand methods

jQuery Ajax Documentation

Indeed an easy solution to this would look like so:

$(document).ready(function(){
  $("#button").click(function(){
    $.get("read.txt",function(data){
      console.log(data); // data contains the contents of read.txt in string format
    });
  });
});
于 2013-06-05T10:19:06.150 に答える