Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/463.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从html中检索值并使用javascript打印_Javascript_Html - Fatal编程技术网

如何从html中检索值并使用javascript打印

如何从html中检索值并使用javascript打印,javascript,html,Javascript,Html,这是我的html代码,我想要一个javascript从文本框中获取值并将其打印在同一页面上 欢迎 名字 姓 出生日期 描述 我厌倦了使用下面显示的javascript以表格形式打印它。。它不起作用,你能推荐一些其他代码吗 val = '' val += 'First Name: '.append("<tr><td>" + document.getElementById('fn').value + "</td></tr&

这是我的html代码,我想要一个javascript从文本框中获取值并将其打印在同一页面上


欢迎
名字
姓
出生日期
描述
我厌倦了使用下面显示的javascript以表格形式打印它。。它不起作用,你能推荐一些其他代码吗

        val = ''
        val += 'First Name: '.append("<tr><td>" + document.getElementById('fn').value + "</td></tr></br>");
        val += 'Last Name: ' .append("<tr><td>" +document.getElementById('ln').value + "</td></tr></br>");
        + '<br>';
        val += 'DOB:  ' + document.getElementById('dob').value + '<br>';
        val += 'Description: ' + document.getElementById('txt').value + '<br>';
        divprint = document.createElement('div')
        divprint.innerHTML = val; document.body.appendChild(divprint)
val=''
val+=“名字:”.append(“+document.getElementById('fn').value+”
”; val+=“姓氏:”.append(“+document.getElementById('ln')。value+”
”; +“
”; val+='DOB:'+document.getElementById('DOB').value+'
'; val+='Description:'+document.getElementById('txt').value+'
'; divprint=document.createElement('div') divprint.innerHTML=val;document.body.appendChild(divprint)
更新

对于其他字段,类似于上述代码:

var elemLn = document.getElementById('ln');
var LnValue = elemLn.value;
document.write('<br/>' + LnValue);

var elemDob = document.getElementById('dob');
var DobValue = elemDob.value;
document.write('<br/>' + DobValue);
var elemLn=document.getElementById('ln');
var LnValue=元素值;
文件。写入(“
”+LnValue); var elemDob=document.getElementById('dob'); 变量DobValue=elemDob.value; 文件。写入(“
”+DobValue);
您可以在
标记之前使用此脚本

<script>

    function validate() {
        return document.getElementById('txt').value;
        // or do something with the value...
    }

</script>

函数验证(){
返回document.getElementById('txt').value;
//或者做一些有价值的事情。。。
}

试试这个

创建一个div并将其附加到Body中,然后将值放在该div中

     function validate() {
      val = ''
            val += document.getElementById('fn').value +'<br>';
           val += document.getElementById('ln').value +'<br>';
        val += document.getElementById('dob').value +'<br>';
             val += document.getElementById('txt').value +'<br>';
     divprint = document.createElement('div')
divprint.innerHTML = val;

document.body.appendChild(divprint)

        }
函数验证(){
val=''
val+=document.getElementById('fn').value+'
'; val+=document.getElementById('ln').value+'
'; val+=document.getElementById('dob').value+'
'; val+=document.getElementById('txt').value+'
'; divprint=document.createElement('div') divprint.innerHTML=val; document.body.appendChild(divprint) }
将此用于表格格式

    val = ''
                    val = "<tr><td>first Name: " + document.getElementById('fn').value +'</td></tr>';
                   val += "<tr><td>Last Name: " +document.getElementById('ln').value +'</td></tr>';
                val += "<tr><td>DOB: " +document.getElementById('dob').value +'</td></tr>';
                     val += "<tr><td>Description: " +document.getElementById('txt').value +'</td></tr>';
             divprint = document.createElement('table')
        divprint.innerHTML = val;
   // divprint.style.border = '1px solid black';
 divprint.border = '1';


        document.body.appendChild(divprint)
val=''
val=“first Name:”+document.getElementById('fn').value+“”;
val+=“姓氏:”+document.getElementById('ln')。值+“”;
val+=“DOB:”+document.getElementById('DOB').value+'';
val+=“Description:”+document.getElementById('txt').value+'';
divprint=document.createElement('表')
divprint.innerHTML=val;
//divprint.style.border='1px纯黑';
divprint.border='1';
document.body.appendChild(divprint)

您的问题中没有javascript(除了调用函数)。。。让我们看看你尝试过什么,你期望什么,什么不起作用。。。我们不会为您这样做,但可以帮助您解决问题。此外,我忍不住要说,基于表格的布局是“所以上千年”,请解释..当您想要获取文本值和print@RonniSkansing绝对不是同一个问题。我当然希望您没有将此标记为重复。如果要在HTML中打印值。。我将以表格格式
文档显示它们。写入
是一种不好的做法。@K.K.Agarwal我正在Visualstudio中编辑它,它在这个codedocument.body.appendChild(divprint)中显示错误。。通过在bodyin document.getElementById(..)之前替换或仅添加document来完成。它显示预期“;”@K.K.Agarwal我可以将值打印在表格
文档中吗。write
是一种糟糕的做法。@GAEfan我同意你的看法,但问题所有者希望这样做function@GAEfan似乎他想要这个函数用于调试目的@ABFORCE我无法使用此代码打印所有元素!它只对你有用one@HareneRangarajan抱歉,我把JS和C搞砸了,用

替换
\n
<div id="emptyDiv"></div>

<script type="text/javascript">
    function validate(){
        document.getElementById("emptyDiv").innerText = document.getElementById("txt").value;
    }
</script>
document.getElementById("emptyDiv").innerText = document.myform.fn.value;
     function validate() {
      val = ''
            val += document.getElementById('fn').value +'<br>';
           val += document.getElementById('ln').value +'<br>';
        val += document.getElementById('dob').value +'<br>';
             val += document.getElementById('txt').value +'<br>';
     divprint = document.createElement('div')
divprint.innerHTML = val;

document.body.appendChild(divprint)

        }
    val = ''
                    val = "<tr><td>first Name: " + document.getElementById('fn').value +'</td></tr>';
                   val += "<tr><td>Last Name: " +document.getElementById('ln').value +'</td></tr>';
                val += "<tr><td>DOB: " +document.getElementById('dob').value +'</td></tr>';
                     val += "<tr><td>Description: " +document.getElementById('txt').value +'</td></tr>';
             divprint = document.createElement('table')
        divprint.innerHTML = val;
   // divprint.style.border = '1px solid black';
 divprint.border = '1';


        document.body.appendChild(divprint)