简体   繁体   English

将表单转换为纯文本

[英]converting form to plain text

I have a page that generates forms depending on user choice. 我有一个页面可以根据用户选择生成表单。 I want to know if there is a way to convert the form into plain text? 我想知道是否可以将表格转换为纯文本吗? Ex. 防爆。 The form has 4 fields, each has a label. 该表单有4个字段,每个字段都有一个标签。 I would like to take all the labels/fields and print them to the user as follows: 我想获取所有标签/字段并按如下所示将它们打印给用户:

label1 field1
label2 field2
label3 field3
label4 field4

Is there a way to do so? 有办法吗?

You can use jQuery to generate basic output, for example: 您可以使用jQuery生成基本输出,例如:

var texts = [];
$("form label").each(function() {
    var oLabel = $(this);
    var oInput = oLabel.next();
    texts.push(oLabel.text() + " - " + oInput.val());
});
var plainText = texts.join("<br />");
$("#Output").html(plainText);

This will iterate over all the form labels, then take the next element of each label which is the input. 这将遍历所有表单标签,然后获取每个标签的下一个元素作为输入。

Live test case . 现场测试用例

If you don't mind an extra file, with PHP. 如果您不介意多余的文件,请使用PHP。

You'd need to make a small php file to echo all the form information like this: 您需要制作一个小的php文件,以回显所有的表单信息,如下所示:

<?php
$label1 = "label1";
$label2 = "label2"; //Set all labels here
$label3 = "label3";
$label4 = "label4";
echo $label1 . " " . $_POST['field1'] . "<br />"; //Change to get depending on your method.
echo $label2 . " " . $_POST['field2'] . "<br />";
echo $label3 . " " . $_POST['field3'] . "<br />";
echo $label4 . " " . $_POST['field4'] . "<br />";
echo "Done.";
?>

and put that in it's own file. 并将其放在自己的文件中。 Set the form's action attribute to that file and it will print out all the data. 将表单的action属性设置为该文件,它将打印出所有数据。

One method, without a JavaSCript library, is: 没有JavaSCript库的一种方法是:

var form = document.getElementsByTagName('form')[0];

form.onsubmit = function(){

    var labels = document.getElementsByTagName('label');

    if (!document.getElementById('container')){
        var container = document.createElement('ol');
        container.id = 'container';
    }
    else {
        var container = document.getElementById('container');
        while (container.firstChild){
            container.removeChild(container.firstChild);
        }
    }

    for (var i=0,len=labels.length; i<len; i++){
        if(document.getElementById(labels[i].getAttribute('for'))){
            var newLi = document.createElement('li');
            var iText = document.createElement('span');
            newLi.innerHTML = labels[i].innerHTML;
            iText.innerHTML = document.getElementById(labels[i].getAttribute('for')).value;
            newLi.appendChild(iText);
            container.appendChild(newLi);
        }
    }

    document.getElementsByTagName('body')[0].appendChild(container);

    return false;

};

JS Fiddle demo . JS小提琴演示

References: 参考文献:

You may get the value for labels and fields in javascript using 您可能会使用javascript获取标签和字段的值

var label1 = getElementById("id").name

Create a string formatted as you want to show the user and show all the values with 创建一个格式化的字符串,以使其显示给用户并显示所有值

document.write(string);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM