【问题标题】:in ajax form do validation and show a hidden div以 ajax 形式进行验证并显示隐藏的 div
【发布时间】:2012-10-30 11:10:13
【问题描述】:

我有这个功能(让我的表单与 ajax 一起工作):

$(function() {
   $('#restore_form').ajaxForm({
   beforeSubmit: ShowRequest,
   success: SubmitSuccesful,
   error: AjaxError
   });
});

function ShowRequest(formData, jqForm, options) {
   var queryString = $.param(formData);
   alert('BeforeSend method: \n\nAbout to submit: \n\n' + queryString);
   return true;
}

function AjaxError() {
   alert("An AJAX error occured.");
}

function SubmitSuccesful(responseText, statusText) {
   alert("SuccesMethod:\n\n" + responseText);
}

我的表单(django 表单)只包含一个文件上传字段。我还想检查验证,为此我有这个功能:

function TestFileType( fileName, fileTypes ) {
   if (!fileName) {
      alert("please enter a file");
      return false; 
   }
   dots = fileName.split(".")
   fileType = "." + dots[dots.length-1];
   if(fileTypes.join(".").indexOf(fileType) != -1){
      alert('That file is OK!') ;
      return true;
   }
   else 
   { 
      alert("Please only upload files that end in types: \n\n" + (fileTypes.join(" .")) + "\n\nPlease select a new file and try again.");
      return false;
   }
}

现在当我尝试在第一个函数中使用验证函数(TestFileType)时,它不起作用。他们两个单独工作。例如,如果我在提交按钮的 onclick 中编写以下行,它可以工作:

onclick="TestFileType(this.form.file.value, ['tar.gz']);"

我还想在成功函数中显示一个隐藏的 div,而不是提醒用户: 我有:

我想在成功函数中做:

 $('.response').html(responseText);
 $('.response').show();

编辑: 这是我的模板:

<form id="restore_form" enctype="multipart/form-data" method="POST" action="restore/">
        {{ form.non_field_errors }}
        {{ form.as_p }}
        {{ form.file.errors }}
        <p id="sendwrapper"><input type="submit" value="{% trans "Send" %}" id="submitButton" style="margin-bottom:10px; cursor:pointer; background-color:#F90;"/></p>            
 </form>
 <div class="response" style="display: none;"></div>

但它不起作用!似乎只有警报在此功能中有效。你能帮我么? 真的谢谢:)

【问题讨论】:

  • 您的表单模板代码是什么样的?你确定 $('.response') 选择器匹配 DOM 中的元素吗?
  • 我编辑了我的问题。我认为它匹配。谢谢:)
  • 从这里看起来不错。也许作为 Firebug 或 Web Inspector 的“网络”选项卡中表单没有导致页面重新加载的健全性检查,它可以解释为什么警报对话框有效,因为警报会阻止页面重新加载,如果您在本地工作,则刷新可能太快以至于不容易被注意到。
  • 我会检查的。但如果页面正在重新加载,因为我的代码中有断点,我会理解的。你能帮我如何使用我的验证函数和 ajax 表单(第一个函数)吗?

标签: javascript jquery django ajaxform


【解决方案1】:

我过去曾尝试使用 AjaxForm 插件,发现除非您有非常具体的原因使用它,否则在没有插件的情况下编写 ajax 表单提交代码通常会更容易。这是我使用没有插件的 Jquery 创建的以前的 jquery ajaxform 的简化/注释版本:

 $('form').submit(function(event) {
    var form = $(this);

    // creates a javascript object of the form data
    // which can be used to validate form data
    var formArray = form.serializeArray();
    // (validate whatever values in formArray you need to check here);

    if (form_is_valid) {
        var formData = form.serialize(); // a URL-encoded version of the form data for the ajax submission
        $.ajax({
            type: "POST",
            url: someUrl,
            data: formData,
            success: function(data) {
                // update success message boxes here
            }
        });
    } else {
        // update client-side validation error message boxes
    }
    event.preventDefault(); // prevent the form from actually navigating to the action page
});

希望这会有所帮助,我发现该插件有时很有用,但我通常发现这会导致更容易理解代码并避免使用插件..

【讨论】:

  • 谢谢,是的,你说得对。我在其他页面中使用过这个功能,但是当我在这里尝试使用它时,它没有发送文件。我的意思是我的请求中没有任何文件发送到服务器。这是一个有用且简单的功能,但它似乎不适用于文件。出于这个原因,我使用了那个插件。
猜你喜欢
  • 1970-01-01
  • 2014-05-22
  • 1970-01-01
  • 1970-01-01
  • 2012-08-31
  • 2012-09-15
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
相关资源
最近更新 更多