如何将此内容输出到文本区域

How can i output this content to a textarea?

本文关键字:输出 文本 区域      更新时间:2024-04-26

我试图将文本从php页面传递到我的html页面,正如Chris Bakers的回答(javascript,而不是jquery)所描述的那样。从javascript 调用php函数

如果我使用普通文本(id=output),但我想将文本输出到文本区域(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>

因为应该使用.value而不是.innerHTML

参考:JavaScript是否通过.value或.innerHTML获取TextArea输入?

它不是setInnerHtml,textarea有一个value属性。不太合乎逻辑,但很好。。。

你疯了:

document.getElementById("bla").value = "test";
<textarea id="bla" readonly >Initial Value</textarea>