简体   繁体   English

使用javascript和表格的动态文本区域

[英]Dynamic text area using javascript and forms

I'm looking for a way to incorporate a bunch of form text inputs, and use the input from the forms to update a text area below the forms. 我正在寻找一种方法来合并一堆表单文本输入,并使用表单中的输入来更新表单下方的文本区域。 The text area will be a chunk of HTML code to embed into the signature of an email. 文本区域将是一块HTML代码,可以嵌入到电子邮件签名中。

For example, a user would enter their name and details into the forms, and after clicking a submit button, the page will generate a piece of HTML code for them to use in their email signature. 例如,用户将其姓名和详细信息输入表单,然后单击提交按钮后,页面将生成一段HTML代码供他们在电子邮件签名中使用。

For the form I might have something like: 对于表单,我可能会有类似的内容:

<label for="EmployeeName">Full name:</label>
<input id="EmployeeName" type="text" maxlength="30" size="20"/>

But I'm not sure how to use the values from the form to update a text field below. 但是我不确定如何使用表单中的值更新下面的文本字段。 I've spent a good chunk of today trying to research this topic, but can't seem to turn anything up. 今天,我花了大量时间尝试研究此主题,但似乎无法解决任何问题。 Any help would be appreciated. 任何帮助,将不胜感激。

For something like this, a library may be nice. 对于这样的事情,图书馆可能是不错的选择。 I personally recommend Knockout, but everyone has their preferences. 我个人推荐淘汰赛,但每个人都有自己的偏好。 This is how I would do it (not necessarily the best way for you). 这就是我的做法(不一定是您的最佳方法)。

demo 演示

Your HTML is pretty much the same, just with an added template. 您的HTML几乎相同,只是添加了一个模板。 The main difference is the data-bind attributes, which tell the elements what data to show. 主要区别在于data-bind属性,该属性告诉元素要显示什么数据。 The valueUpdate key, says to update the variable after keydown , rather than blur (oposite of focus ). valueUpdate键说,更新后的变量keydown ,而不是blur (的oposite focus )。

<div>
<label for="EmployeeName">Full name:</label>
<input id="EmployeeName" type="text" maxlength="30" size="20" 
    data-bind="value: EmployeeName, valueUpdate: 'afterkeydown'" />
<label for="XYZ">A Label:</label>
<input id="XYZ" type="text" maxlength="30" size="20" 
    data-bind="value: XYZ, valueUpdate: 'afterkeydown'" />
</div>

<textarea disabled rows=20 data-bind="value: Email"></textarea>

<script type="text/html" id="signatureTemplate">
<h2>EmployeeName</h2>
<h2>XYZ</h2>
<h3>Company</h3>
<h3>My Position</h3>
</script>

Those data-bind values match up with your JavaScript, and are updated every time the user types a character. 这些data-bind值与您的JavaScript匹配,并且每次用户键入字符时都会更新。 You can learn Knockout on their webiste , or just modify this code as needed. 您可以在他们的网站上了解淘汰赛,也可以根据需要修改此代码。

var SignatureGenerator = function(){
    var self = this;

    self.EmployeeName = ko.observable('');
    self.XYZ = ko.observable('');

    self.Email = ko.computed(function(){
        return document.getElementById("signatureTemplate").innerHTML
        .replace("EmployeeName", self.EmployeeName());
        .replace("XYZ", self.XYZ());
    });
}

ko.applyBindings(window.app = new SignatureGenerator());

Not sure if this is what you mean, but this could be a solution: 不知道这是否是您的意思,但这可能是一个解决方案:

$('#done').click(function(e){
    e.preventDefault();
    var tag = "<p>";
    var split = tag.substring(1);
    $('#res').val(tag+$('#EmployeeName').val()+'</'+split);
});

Where the HTML: HTML所在的位置:

<label for="EmployeeName">Full name:</label>
<input id="EmployeeName" type="text" maxlength="30" size="20"/>
<button id="done">Done</button>
<textarea id="res"></textarea>

Check it out here: Fiddle 在这里查看: 小提琴

The code basically is saying: 该代码基本上是在说:

  • $('#done').click on click of the button $('#done').click按钮的单击
  • e.preventDefault(); don't do the usual action of sending the form but do what I say. 不要执行发送表单的常规操作,而是按照我说的做。
  • var tag = "<p>" chooses the tag you want to add (paragraph) var tag = "<p>"选择要添加的标签(段落)
  • var split = tag.substring(1); removes the first < so we can later add a slash 删除第一个<这样我们以后可以添加斜杠
  • $('#res').val( gives the value to the id res of what is in the parenthesis wichi is the tag, the input the </ and the split: tag+$('#EmployeeName').val()+'</'+split $('#res').val(将值添加到括号中的id res值中,即标记,输入</和拆分: tag+$('#EmployeeName').val()+'</'+split

Not entirely sure if I understand the question, but this is what I came up with (using jQuery): 不确定我是否理解这个问题,但这是我想出的(使用jQuery):

// when the go button is clicked
$('#action').click(function() {
    // build up some html based on the input values
   var html = '<h1>' + $('#EmployeeName').val() + '</h1>'
   html += '<h2>' + $('#EmployeeSurname').val() + '</h2>'
   // set it in the textarea
   $('#result').html(html);
});

Should not be that hard to find documentation about online... 不难找到有关在线的文档...

I set up a small fiddle to demonstrate: http://jsfiddle.net/jQ2Ya/ 我设置了一个小提琴来演示: http : //jsfiddle.net/jQ2Ya/

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

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