我想使用javascript在同一页面上显示表单数据 [英] I want to display form data on same page using javascript

查看:59
本文介绍了我想使用javascript在同一页面上显示表单数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要完整的html,javascript代码,用于在同一页面上显示表单数据



我尝试过:







< title>测试输入



功能testVariable(){

var strText = document.getElementById(textone);

document.write(strText.value);

var strText1 = document.getElementById(textTWO);

document.write(strText1.value);

}















i want full html,javascript code for displaying form data on same page

What I have tried:



<title>Test Input

function testVariable() {
var strText = document.getElementById("textone");
document.write(strText.value);
var strText1 = document.getElementById("textTWO");
document.write(strText1.value);
}







推荐答案

不要使用document.write。如果要向页面添加内容,则需要使用DOM。谷歌创建div javascript或创建跨度javascript作为示例,您基本上需要创建一个包含文本的元素,并将该元素添加到您希望文本显示的页面部分。



另外还有div \ span你希望文本显示在页面上已经只是更新它



Don't use document.write. If you want to add content to a page you need to work with the DOM. Google "create div javascript" or "create span javascript" for examples, you basically need to create an element that has your text in it and add that element to the part of the page you want the text to display.

Alternatively have the div\span you want the text to appear at already on the page and just update it

document.getElementById("myDiv").innerHTML = strText.value;


代码,

Code,
<!DOCTYPE html>
<html>
<head>
    <script>
        function testVariable() {
            var strText = document.getElementById("textone").value;          
            var strText1 = document.getElementById("textTWO").value;
            var result = strText + ' ' + strText1;
            document.getElementById('spanResult').textContent = result;
             
        }
    </script>
</head>
<body> 
    <input type="text" id="textone" />
    <input type="text" id="textTWO" />
    <button  onclick="testVariable()">Submit</button> <br />
    <span id="spanResult">

    </span>
   
     
</body>
</html>


<form name="frm">
     <input name="name" type="text" id="name" placeholder="Enter Your Name" required>
 
<input name="number" id="number" type="text" pattern="[789][0-9]{9}" placeholder="Enter Your Mobile Number" required>  

  <button style="font-size: 22px;" value="submit" onClick="myFunction()" type="submit">Submit</button>
    
            <div id="popcode"></div>
          </form>





函数myFunction(){

var a,b, currdate,d,n;

a = document.frm.text.value;

b = a.substring(0,2);

var currdate = new Date();



var currdate = currdate.getDate()+''+(currdate.getMonth()+ 1)+''+ String( currdate.getFullYear())。substr(2);

var d = new Date();

var n = d.getSeconds();

document.getElementById(popcode)。innerHTML = b + currdate + n;



}



function myFunction() {
var a,b,currdate,d,n;
a= document.frm.text.value;
b=a.substring(0,2);
var currdate = new Date();

var currdate= currdate.getDate() + '' + (currdate.getMonth()+ 1) + '' + String(currdate.getFullYear()).substr(2);
var d = new Date();
var n = d.getSeconds();
document.getElementById("popcode").innerHTML =b+currdate+n;

}


这篇关于我想使用javascript在同一页面上显示表单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆