js

用js做一个添加记录,要求:页面上有一文本域,一文本框 和保存按钮。在文本框中添加内容,点击保存,此内容显示在文本域中,再在文本框中填写内容,点击保存,此内容也显示在文本域中,前后两条记录分行显示,怎么做?

解决方案 »

  1.   

    不要用textarea,用div
    var d = document.getElementById(divId);d.innerHTML += '第一行' + '<br />';
    d.innerHTML += '第二行' + '<br />';
      

  2.   

    你那样做的话,实现如下:
    每次点击按钮的时候,分别获取text和textarea里面的内容,将textarea的内容重写,中间添加换行符合就可以了。
    <html> 
    <head> 
    <script> 
    function updateValue(){
        var textValue = document.getElementById("text1").value;  
        alert(textValue);
        var textareaValue = document.getElementById("contactus").value; 
        alert(textareaValue);
        document.getElementById("contactus").value=textareaValue+"\n"+textValue;
    }
    </script> 
    </head> 
    <body >
    <form name="form1">
    <table cellpadding="0" cellspacing="0" border="0" width="100%" > 
    <tr> 
    <td> 
       <input type="text" name="text1" value=""/>
       <textarea cols="5" rows="5" id="contactus" name="contactus">

    </textarea><input type="button" name="button1" onclick="updateValue()">
    </td>
    </tr> 
    </table> 
    </form>
    </body> 
    </html> 
      

  3.   

    改了一下楼上的 楼上的没考虑完全 会有空格 
    <html>  
    <head>  
    <script>  
    function updateValue(){ 
        var textValue = document.getElementById("text1").value;   
        var textareaValue = document.getElementById("contactus").value; 
    if(textareaValue!=""){
     document.getElementById("contactus").value=textareaValue+"\r"+textValue; 
     }else{
    document.getElementById("contactus").value += textValue;
     }

    </script>  
    </head>  
    <body > 
    <form name="form1"> 
    <table cellpadding="0" cellspacing="0" border="0" width="100%" >  
    <tr>  
    <td>  
    <input type="text" name="text1" value=""/> 
    <textarea cols="20" rows="5" id="contactus" name="contactus"></textarea> <input type="button" name="button1" onclick="updateValue()"> 
    </td> 
    </tr>  
    </table>  
    </form> 
    </body>  
    </html>