如何使用jquery validate远程验证来设置和返回表单字段值到表单操作页面? [英] How do I use jquery validate remote validation to set and return form field values to the form action page?

查看:399
本文介绍了如何使用jquery validate远程验证来设置和返回表单字段值到表单操作页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,验证使用jQuery插件:验证,v1.9.0。验证还执行对服务器端脚本(Coldfusion)的远程调用以检查用户名,电子邮件地址等。

I have a form that validates using the jQuery plugin: Validation, v1.9.0. The validation also performs remote calls to a server side script (Coldfusion) to check user names, email addresses, etc..

我的问题是:

在远程验证过程中,有时通过服务器端脚本定义和设置新的表单字段,需要将它们返回到调用表单,以便将它们传递给表单的动作页面。

During the remote validation process, there are times when new form fields will be defined and set through the server side script which need to be returned to the calling form so that they can be passed to the form's action page.

正如你会注意到,在下面的代码片段中,我必须通过流程的每个步骤走这些表单字段值(即,创建结果结构,在成功处理程序中挑选那些),这既乏味又容易出错,因为形式可能会在将来更改。

As you'll notice in my code snippets below, I am having to walk these form field values through each step of the process (i.e., creating a result structure, picking those up in the success handler, etc.) which is both tedious, and prone to errors as the form may change in the future.

方法我可以使用它将简单地允许我全局设置这些表单字段值,我的调用形式/操作页面可以通过一旦形式验证?

Is there a method I can use which will simply allow me to "globally" set these form field values which my calling form/action page can pickup through once the form validates?

JQUERY VALIDATION SNIPPET:

validator = $('#cartDownload_form').validate({

  submitHandler: function(form) {

  // make submit button disabled
  $('#submit_button').attr('disabled', 'disabled');

  // show modal dialog about submittal process..
  var processing_dialog = ShowDialog("Processing Order...", "<p>Please wait while we process your order...</p>", false);

  // prepare Options Object 
  var options = { 
    url: "/products/val_cartDownload_remote.cfm", 
    dataType: 'json',
    type: "POST",
    success: function(data) { 

      if(data.ERRORMESSAGELIST.length == 0) {
        // no errors...

        // set return form field values needed for action page
        $('#verified').val(data.VERIFIED);
        $('#dlurl').val(data.DLURL);
        $('#dllocation').val(data.DLLOCATION);
        $('#itemname').val(data.ITEMNAME);
        $('#itemtease').val(data.ITEMTEASE);
        $('#itemimage').val(data.ITEMIMAGE);
        $('#itemimage_border').val(data.ITEMIMAGE_BORDER);
        $('#itemimage_alt').val(data.ITEMIMAGE_ALT);
        $('#itemimage_title').val(data.ITEMIMAGE_TITLE);
        $('#itemthumb').val(data.ITEMTHUMB);
        $('#itemthumb_border').val(data.ITEMTHUMB_BORDER);
        $('#itemthumb_alt').val(data.ITEMTHUMB_ALT);
        $('#itemthumb_title').val(data.ITEMTHUMB_TITLE);
        $('#itempubnotes').val(data.ITEMPUBNOTES);
        $('#itemurl_size').val(data.ITEMURL_SIZE);  
        $('#first_name').val(data.FIRST_NAME);  

        // submit the form
        $('#cartDownload_form')[0].submit();  

        return true;

      } else {
        // an error occurred...

  }; 

  $(form).ajaxSubmit(options);

  return false;
}

REMOTE VALIDATION SNIPPET(这些是新的表单字段,调用表单):

    <cfset variables.result_struct = {
  errorfieldlist = listtoarray(form.errorfieldlist),
  errormessagelist = listtoarray(form.errormessagelist, form.RS),
  verified = form.verified,
  dlurl = form.dlurl,
  dllocation = form.dllocation,
  itemname = form.itemname,
  itemtease = form.itemtease,
  itemimage = form.itemimage,
  itemimage_border = form.itemimage_border,
  itemimage_alt = form.itemimage_alt,
  itemimage_title = form.itemimage_title,
  itemthumb = form.itemthumb,
  itemthumb_border = form.itemthumb_border,
  itemthumb_alt = form.itemthumb_alt,
  itemthumb_title = form.itemthumb_title,
  itempubnotes = form.itempubnotes,
  itemurl_size = form.itemurl_size,
  first_name = form.first_name
}>

<cfoutput>#serializeJSON(result_struct)#</cfoutput>

因此,我想要避开的是手动设置所有这些表单字段值在几个地方只是为了让他们回到我的调用表单/操作页面。

So what I'm trying to get away from is having to set all of these form field values manually in several places simply to get them back to my calling form/action page.

我可能是一个简单的菜鸟错误,我似乎找不到答案。

I'm probably making a simply rookie mistake and I can't seem to find the answer.

感谢。

推荐答案

p>

To simplify your jQuery:

if(data.ERRORMESSAGELIST.length == 0) {
// no errors...
// set return form field values needed for action page
    for (field in data)        {
      $('#' + field.toLowerCase()).val(data[field]);
    }

    // submit the form
    $('#cartDownload_form')[0].submit();  

而您的cf代码:

  <cfset variables.result_struct = {
    errorfieldlist = listtoarray(form.errorfieldlist),
    errormessagelist = listtoarray(form.errormessagelist, form.RS)
  }>
  <cfset StructAppend(variables.result_struct, form)>

<cfoutput>#serializeJSON(result_struct)#</cfoutput>

这篇关于如何使用jquery validate远程验证来设置和返回表单字段值到表单操作页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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