从文本文件填充textArea

时间:2018-10-17 09:48:34

标签: javascript html xmlhttprequest

我有一个简单的文本文件:

Text1
Text2
Text3
TextN

并且想要创建一个界面,在该界面中,每次用户按下next按钮时,文本区域将填充下一个文本。我想我必须为此使用数组,但是我在使用下一步按钮更改文本时遇到问题。而且,我拥有的代码仅在Firefox上有效。这是否意味着当我将网站放在服务器上时,将仅由Firefox客户端解释?

function test() {
  var xmlhttp;
  if (window.XMLHttpRequest) {
    xmlhttp = new XMLHttpRequest();
  } else {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4) {
      var contents = xmlhttp.responseText;
      myArray = contents.split("\n");

    }
  }
  xmlhttp.open("GET", "deeds.txt", true);
  xmlhttp.send();
}


function nextDeed() {
  document.getElementById('deed').innerHTML = myArray[0]
}
<html>
<body>
  <h1 align="center" style="font-family:Arial;">RELATION TAGGING </h1>
  <textarea id="deed" rows="15" cols="160"></textarea>
  <input type="button" value="next" onclick="nextDeed()" /><br/>
  <div id="result">Result</div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

step = 0;

$("#next").click(function(){
		  step = step +1;
	      
	      $.ajax({
			  url: "./text.php?"+"step="+step,
			  success: function(data){
			    $("#deed").val(data);
			  }
		});
	});

	;
<html>
<head>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>

<h1 align="center" style="font-family:Arial;" >RELATION TAGGING </h1>
	<textarea id="deed" rows="15" cols="160">
	</textarea>
	<input type="button" id="next" value="next" /><br/>
	<div id="result">Result</div>
</body>



</html>

PHP code : <?php $data  = file_get_contents("file".$_GET["t"].".txt"); echo $data;?>
相关问题