2016-07-06 9 views
0

私は、Ajaxを使用してPOSTに設定したフォームを持っています。現在、私の検証では、基本的には、この機能である:これは正常に動作しますAjax POSTによる検証

// Function for making all fields required 
$(function(){ 
    $("input").prop('required',true); 
}); 

、しかし、私のフォームの部分が選択された前回の回答に応じて、CSSを使って隠されているので、 - 私は、コンソール内のすべての隠されたフィールドに次のエラーを取得する:

​​

もっと簡単にAjaxフォームに検証を追加する方法はありますか?

など。一部のフィールドは、リンクを必要とする - 私は入力が「リンク構造」私のAjaxのPOSTコードの

例があることを検証することができると思う:役立つフォームにNOVALIDATE属性を追加する

// Wait for the DOM to be loaded 
$(document).ready(function() { 

    // Make sure New Provider is unchecked by default 
    document.getElementById("typeNew").checked = false; 

    // Variable to hold request 
    var request; 

    // Bind function to form submit event 
    $("#createPanelForm").submit(function(event){ 

     // Abort any pending request 
     if (request) { 
      request.abort(); 
     } 

     // Setup local variable 
     var $form = $(this); 

     // Select and cache form fields 
     var $inputs = $form.find("projectName, projectLink, type, providerName, completeLink, quotaFullLink, screenoutLink, existingProvider"); 

     // Serialize the data in the form 
     var serializedData = $form.serialize(); 

     // Disable the inputs for the duration of the Ajax request 
     // Disabled form inputs will not be serialized. 
     $inputs.prop("disabled", true); 

     // Send the request 
     request = $.post({ 
      url: "actions/createpanel.action.php", 
      method: "POST", 
      data: serializedData, 
      function(result){ 
       alert(result); 
      } 
     }); 

     // Callback handler for successful request 
     request.done(function (response, textStatus, jqXHR){ 
      // Log data to console 
      console.log(serializedData); 
      console.log(response); 
      document.getElementById('submitSuccess').style.display = 'block'; 
     }); 

     // Callback handler for failed request 
     request.fail(function (jqXHR, textStatus, errorThrown){ 
      // Log the error to console 
      console.error("Error: "+textStatus, errorThrown); 
      document.getElementById('submitError').style.display = 'block'; 
     }); 

     // Callback handler for both outcomes 
     request.always(function() { 
      // Reenable the inputs 
      $inputs.prop("disabled", false); 
     }); 

     // Prevent default form posting 
     event.preventDefault(); 

     // Hide form and show response 
     $("#createPanelForm").fadeOut(1000); 
     $("#submitResponse").delay(1001).fadeIn(1000);  
    }); 

}) 
+0

jQuery検証プラグインを使用https://jqueryvalidation.org/ –

+1

可能な複製http://stackoverflow.com/questions/30644606/an-invalid-form-control-with-name-is-not-focusable-何も必要ない-hなし –

+0

@AneesSabanリンクをチェックアウトしましたが、私の質問には全く関係していないようです。私は、必要な属性に頼るよりも適切なバリデーションを行うことにもっと興味があります –

答えて

0

を:

<form name="myform" novalidate> 
+1

例やスニペットを投稿することができればもっと良いでしょう。あるいはこれもコメントである可能性があります。 –

関連する問題