我试图将文本从php页面传递到我的html页面,如Chris Bakers的回答(javascript,而不是jquery)。 Call php function from javascript
代码有效,如果我使用普通文本(id =输出),但我想将文本输出到textarea(id = text1)而不是普通文本,只是更改id不起作用。
这是我的代码:
<html>
<head>
</head>
<body>
<textarea id="text1" style="background-color: #969696" cols="50" rows="10" readonly></textarea>
<div id="output">waiting for action</div>
</body>
<script>
function getOutput() {
var value = document.getElementById("artikelnr").value;
var file = selectedValue()+".csv";
getRequest(
"verarbeitung.php?eingabe="+value+"&eingabe2="+file, // URL for the PHP file
drawOutput, // handle successful request
drawError // handle error
);
return false;
}
// handles drawing an error message
function drawError() {
var container = document.getElementById('text1');
container.innerHTML = 'Bummer: there was an error!';
}
// handles the response, adds the html
function drawOutput(responseText) {
var container = document.getElementById('text1');
container.innerHTML = responseText;
}
// helper function for cross-browser request object
function getRequest(url, success, error) {
var req = false;
try{
// most browsers
req = new XMLHttpRequest();
} catch (e){
// IE
try{
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
// try an older version
try{
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
return false;
}
}
}
if (!req) return false;
if (typeof success != 'function') success = function () {};
if (typeof error!= 'function') error = function () {};
req.onreadystatechange = function(){
if(req.readyState == 4) {
return req.status === 200 ?
success(req.responseText) : error(req.status);
}
}
req.open("GET", url, true);
req.send(null);
return req;
}
</script>
</html>
答案 0 :(得分:1)
因为您应该使用.value
而不是.innerHTML
。
答案 1 :(得分:1)
它不是setInnerHtml,textarea有一个value属性。不太合乎逻辑但很好......
你生一个小提琴:
document.getElementById("bla").value = "test";
&#13;
<textarea id="bla" readonly >Initial Value</textarea>
&#13;