使用 JavaScript 实时读取和附加表单数据

发布于 2024-09-19 02:14:20 字数 1062 浏览 0 评论 0原文

我一直在寻找这个问题的解决方案一段时间,并且必须找到一个对新手足够友好的答案,让我能够理解其实现。我的情况如下:

我正在创建一个简单、小型、基于 Web 的文档编号系统,该系统将输入表单的数据组合起来形成文档编号。示例如下:用户输入类别代码 (CCC)、基本号码 (BBBB) 和短划线号码 (DDD)。生成的文档编号将为 CCC-BBBB-DDD。超级简单。我让它将所有这些写入数据库以及所有这些爵士乐。我只想添加一个用户友好的附加组件。

我想要在顶部有一个实时生成的小字符串,显示用户在实际按下提交之前编辑每个字段时的文档编号。有点像这个例子: http://inimino.org/~inimino/blog/javascript_live_text_input

我对 javascript 几乎一无所知,因此了解 1:脚本应该是什么样子,2:以及该脚本如何与 html 表单交互将非常有帮助。

表格如下所示:

        <form action="submit.php" method="post">
        Enter Title:<input type="text" name="title" size="20"><BR>
        Enter Class Code:<input type="text" name="class" size="20"><BR>
        Enter Base Number:<input type="text" name="base" size="20"><BR>
        Enter Dash Number:<input type="text" name="dash" size="20"><BR>
        <input type="submit" value="Submit">

非常感谢您提供的任何帮助。我确信这对于精通的人来说并不是太难。

托马斯

I have been searching for the solution to this problem for a while and have to come across an answer that is newbie-friendly enough for me to understand its implementation. Heres my situation:

I am creating a simple, little, Web-based document numbering system that takes data entered into a form and combines it to form a document number. An example would be: A user enters a, Class Code(CCC), Base Number(BBBB), and a Dash number (DDD). The resulting document number would be CCC-BBBB-DDD. Super simple. I have it writing all of this to the database and all that jazz. I would just like to add one user friendly add on.

I want a little live-generate string at the top that shows what the Document number will be as the user edits each field before they actually press submit. Kinda like this example: http://inimino.org/~inimino/blog/javascript_live_text_input

I know almost nothing about javascript so it would be really helpful to know, 1: what the script should look like, 2: And How that script is interfacing with the html form.

Heres what the form looks like:

        <form action="submit.php" method="post">
        Enter Title:<input type="text" name="title" size="20"><BR>
        Enter Class Code:<input type="text" name="class" size="20"><BR>
        Enter Base Number:<input type="text" name="base" size="20"><BR>
        Enter Dash Number:<input type="text" name="dash" size="20"><BR>
        <input type="submit" value="Submit">

Thanks so much for any help you can offer. I'm sure this isn't too hard for someone well versed.

Thomas

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

递刀给你 2024-09-26 02:14:20

据我了解,这应该按照您的描述进行。

$('#yourForm input').bind('keyup', function(e) {
    var docNum = 'Your Document Number: <br/>'+$('input[name="class"]').val() + '-' + $('input[name="base"]').val() + '-' + $('input[name="dash"]').val();
    $('#preview').html(docNum);
});​

对于第二个问题,它如何与 HTML 表单交互。第一个 jQuery 选择器 #yourForm input 将查找属于

的任何 代码>.然后它绑定 keyup 事件来触发该函数。该函数从 获取值,其中包含 class、base 和 dash 的 name 值以及一些格式,并创建一个名为 docNum< 的变量/代码>。然后,将 docNum 插入到将 id 设置为预览的元素中,在 Fiddle 示例中,该元素是表单正上方的 div

http://jsfiddle.net/nuY2M/

From what I'm understanding this should do what you describe.

$('#yourForm input').bind('keyup', function(e) {
    var docNum = 'Your Document Number: <br/>'+$('input[name="class"]').val() + '-' + $('input[name="base"]').val() + '-' + $('input[name="dash"]').val();
    $('#preview').html(docNum);
});​

For your second question, in how it interfaces with the HTML form. The first jQuery selector #yourForm input is going to look for any <input> that falls under a <form id='yourForm'>. It's then binding the keyup event to fire the function. The function takes the value from the <input> with the name value of class, base and dash as well as some formatting and creates a variable named docNum. docNum is then inserted into the element with the id set to preview, which in the Fiddle example is a div right above the form.

http://jsfiddle.net/nuY2M/

树深时见影 2024-09-26 02:14:20

在您希望显示文档编号预览的位置包含此 html:

Document #:
<span id="classPreview"></span>
-
<span id="basePreview"></span>
-
<span id="dashPreview"></span>

添加此脚本以填充值:

function updateDocNumPreviewPart(fieldName)
{
    var preview = document.getElementById(fieldName + "Preview");
    var field = document.forms[0][fieldName];
    preview.innerHTML = field.value;
}
function updateDocNumPreview()
{
    updateDocNumPreviewPart("class");
    updateDocNumPreviewPart("base");
    updateDocNumPreviewPart("dash");
}

最后,向表单字段添加一些代码以调用脚本:

<input ... onkeyup="updateDocNumPreview()" onchange="updateDocNumPreview()" />

Include this html where you want the document number preview to display:

Document #:
<span id="classPreview"></span>
-
<span id="basePreview"></span>
-
<span id="dashPreview"></span>

Add this script to populate the values:

function updateDocNumPreviewPart(fieldName)
{
    var preview = document.getElementById(fieldName + "Preview");
    var field = document.forms[0][fieldName];
    preview.innerHTML = field.value;
}
function updateDocNumPreview()
{
    updateDocNumPreviewPart("class");
    updateDocNumPreviewPart("base");
    updateDocNumPreviewPart("dash");
}

Finally, add some code to your form fields to call the script:

<input ... onkeyup="updateDocNumPreview()" onchange="updateDocNumPreview()" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文