如何将我在表单中输入的HTML表单值获取到txt文件中? [英] How can I get the HTML form value which I entered in the form into the txt file?

查看:80
本文介绍了如何将我在表单中输入的HTML表单值获取到txt文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<!DOCTYPE html> 
< html>
< head>
< style>
form * {
display:block;
保证金:10px;
}
< / style>
< script language =Javascript>
函数下载(文件名,文本){
var pom = document.createElement('a');
pom.setAttribute('href','data:text / plain; charset = utf-8,'+

encodeURIComponent(text));
pom.setAttribute('download',filename);

pom.style.display ='none';
document.body.appendChild(pom);

pom.click();

document.body.removeChild(pom);


}





< / script>
< / head>
< body>
< h1>注册表< / h1>

< input type =textname =namevalue =test.txt>
< form onsubmit =download(this ['name']。value,this ['text']。value)>


< p>
< label for =fname>名称< / label>
< input type =textid =Fullnamename =Fullnameplaceholder =Your name ..>

< / p>


< p>
< label for =lname>电子邮件ID< / label>
< input type =textid =EmailIdname =EmailIdplaceholder =Enter Valid Email Id>

< / p>

< p>
< label for =City> City< / label>
< select id =Cityname =City>
< option value =Chennai> chennai< / option>
< option value =Bangalore> Bangalore< / option>
< option value =Hyderabad> Hyderabad< / option>
< option value =Delhi> Delhi< / option>
< option value =Calcutta> Calcutta< / option>
< option value =Mumbai> Mumbai< / option>
< / select>

< / p>

< p>
< label for =Gender>性别< / label>
< input type =radioname =radiovalue =Maleonclick =getElementById('problem')。value = this.value;>男性<峰; br>
< input type =radioname =radiovalue =Femaleonclick =getElementById('problem')。value = this.value;>女性<峰; br>

< input type =textname =problemid =problem>

< / p>

< label for =Queries>查询< / label>
< textarea id =Queriesname =textplaceholder =Write your Queries> < / textarea的>



< input type =submitvalue =SAVE>
< / form>
< / body>
< / html>





我尝试过:



我在Textarea中输入的值是文本文件中的diplaying ..但我应该从表单中获取所有值到记事本...请输入此...

解决方案

将表单元素传递给函数,并迭代其元素集合以构建文本:

 <  表格    onsubmit   = 下载(此['name']。value,this) >  



< pre lang =Javascript> function 下载(文件名,表格){
var lines = [];
var elements = form.elements;
for var i = 0 ; i< elements.length; i ++){
var field = elements [i];
if (!field.name){ continue ; }

switch (field.type){
case < span class =code-string> radio
case 复选框:{
if (field.checked){
lines.push(field.name + + field.value);
}
break ;
}
默认:{
lines.push(field.name + + field.value);
break ;
}
}
}

var text = lines.join( \ n);
var pom = document .createElement(' a');
pom.setAttribute(' href' ' data:text / plain; charset = utf-8,' + encodeURIComponent(text));
pom.setAttribute(' download',filename);
pom.style.display = ' none';
document .body.appendChild(pom);
pom.click();
document .body.removeChild(pom);
}



演示 [ ^ ]


 <!DOCTYPE html>
<html>
<head>
    <style>
        form * {
            display: block;
            margin: 10px;
        }
    </style>
    <script language="Javascript">
function download(filename, text) {
  var pom = document.createElement('a');
  pom.setAttribute('href', 'data:text/plain;charset=utf-8,' +

encodeURIComponent(text));
        pom.setAttribute('download', filename);

        pom.style.display = 'none';
        document.body.appendChild(pom);

        pom.click();

        document.body.removeChild(pom);

 
        }

       



    </script>
</head>
<body>
    <h1>Registration Form</h1>

      <input type="text" name="name" value="test.txt">
    <form onsubmit="download(this['name'].value, this['text'].value)">


        <p>
            <label for="fname">Name</label>
            <input type="text" id="Fullname" name="Fullname" placeholder="Your name..">
          
        </p>


        <p>
            <label for="lname">Email Id</label>
            <input type="text" id="EmailId" name="EmailId" placeholder="Enter Valid Email Id">
           
        </p>

        <p>
            <label for="City">City</label>
            <select id="City" name="City">
                <option value="Chennai">chennai</option>
                <option value="Bangalore">Bangalore</option>
                <option value="Hyderabad">Hyderabad</option>
                <option value="Delhi">Delhi</option>
                <option value="Calcutta">Calcutta</option>
                <option value="Mumbai">Mumbai</option>
            </select>
           
        </p>

        <p>
            <label for="Gender">Gender</label>
            <input type="radio" name="radio" value="Male" onclick="getElementById('problem').value=this.value;"> Male<br>
            <input type="radio" name="radio" value="Female" onclick="getElementById('problem').value=this.value;"> Female<br>

            <input type="text" name="problem" id="problem">
            
        </p>

        <label for="Queries">Queries</label>
        <textarea id="Queries" name="text" placeholder="Write your Queries"> </textarea>


        
            <input type="submit" value="SAVE">
        </form>
</body>
</html>



What I have tried:

The values which i entered in the Textarea alone is diplaying in text file.. But i should get all the values from the form into the Notepad... pls Sort out this...

解决方案

Pass the form element to the function, and iterate over its elements collection to build the text:

<form onsubmit="download(this['name'].value, this)">


function download(filename, form) {
    var lines = [];
    var elements = form.elements;
    for (var i = 0; i < elements.length; i++) {
        var field = elements[i];
        if (!field.name) { continue; }

        switch (field.type) {
            case "radio":
            case "checkbox": {
                if (field.checked) {
                    lines.push(field.name + ": " + field.value);
                }
                break;
            }
            default: {
                lines.push(field.name + ": " + field.value);
                break;
            }
        }
    }
    
    var text = lines.join("\n");
    var pom = document.createElement('a');
    pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
    pom.setAttribute('download', filename);
    pom.style.display = 'none';
    document.body.appendChild(pom);
    pom.click();
    document.body.removeChild(pom);
}


Demo[^]


这篇关于如何将我在表单中输入的HTML表单值获取到txt文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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