通过遍历表单字段来建立变量的值 [英] Building value of a variable by looping through form fields

查看:57
本文介绍了通过遍历表单字段来建立变量的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

概述

我正在为特定页面构建表单,用户可以在其中为任何页面的内容提出建议.

I'm building a form for a specific page, where users can make suggestions about the contents of any page.

该表单显示在模式对话框中,并且允许用户提出建议而不会离开他们所在的页面.

The form is displayed inside modal dialogue box, and allows the user to make their suggestions without being taken away from the page they were on.

当然,我正在设计可重复使用的代码-编写一次,使用很多代码-在站点上的任何地方提供一种类似的表单显示和提交方法,而无需重新编写代码.

Of course, I'm designing the code for reuse - write once, utilise many - to provide a similar method of form display and submission anywhere on the site without having to re-write code.

环境

该站点托管在IIS6服务器上,页面位于ASP中,并使用

The site is hosted on an IIS6 server, pages are in ASP, and sending the form data via email using the trusty, classic ASP FormMail script from BrainJar.

问题

模式对话框有效,使用来自Nettuts + 的示例,用于从模式中执行提交.

The modal dialogue box works a treat, and using this example from Nettuts+ to perform the submit from within the modal.

Nettuts +脚本的构建方式是,您需要定义构成表单的所有单个字段,并将其传递给Formmail脚本.

The Nettuts+ script is built it such a way that you need to define all the individual field that make up your form to be passed to your formmail script.

因此,每次需要使用它时,您都需要更新var dataString的组合方式,这取决于您拥有的咖啡因量或痛苦的用户数量(使您无法正常使用)使您轻松自如错误可能需要数小时才能诊断和纠正.

Thus each time you need to use it, you'll need to update how var dataString is combined, which depending on how much caffine you've had or the number of painful users knocking down your door leaves you open to simple mistakes that can take hours to diagnose and correct.

然后将每个字段编译成一个称为dataString的变量:

Each field is then compiled into a variable known as dataString:

var dataString = 'name='+ name + '&email=' + email + '&phone=' + phone;

然后,Ajax启动,并将数据提交到脚本,并根据结果将在定义的div中显示成功消息:

Ajax then kicks in, and submits the data to the script, and depending on the result a success message will appeared within the defined div:

$.ajax({
  type: "POST",
  url: "/_global/assets/scripts/formmail/formmail.asp",
  data: dataString,
  success: function() {
    $('#contact_form').html("<div id='message'></div>");
    $('#message').html("<h2>Suggestion received</h2>")
    .append("<p>Thanks, your suggestion has been received and
    will be actioned as soon as possible.</p>")
    .hide()
    .fadeIn(1500, function() {
      $('#message').append("<img id='checkmark' src='images/check.png' />");
    });
  }
....

所需结果

使这段代码可全局重用,并使用循环将var dataString填充表单中的所有字段名称和值.

Make this piece of code globally reusable, and have var dataString populated with all field names and values from the form using a loop.

推荐答案

Er-您是否正在使用$('#contact_form').serialize(),可以在data:之后追加$('#contact_form').serialize(),所以结果是data: $('#contact_form').serialize()还是我被您的疑问措词所误解?

Er - are you looking to use $('#contact_form').serialize() which you can append after data: so the result is data: $('#contact_form').serialize() or am I mistaken by your question wording?

这篇关于通过遍历表单字段来建立变量的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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