2013-10-02 61 views
0

我有一个jquery,在完整的提交和因为提交发生两次。有没有办法避免这种情况?jquery玩框架避免双重提交

以下是代码片段。


我的路线索赔加载看起来像这样。

# Claim Loading for Historical Claims 
GET  /claimLoading   controllers.ClaimLoading.form 
POST /claimLoading   controllers.ClaimLoading.submit 

在我的控制器提交情况,如图所示。

/** 
* Handle form submission. 
*/ 
def submit = Action { implicit request => 
claimLoadingForm.bindFromRequest.fold(
    // Form has errors, redisplay it 
    errors => { 
    Logger.info("Some error occurred before calling the service") 
    BadRequest(html.claimloading.form(errors)) 
    }, 
    claimLoading => { 
    // Invoke the LoadCSVorXML2Mongo service from here 
    claimsLoadingService.loadCSVOrXMLClaimToDatabase(claimLoading.claimLoadingPath) 

    val resultSummary = claimsLoadingService.retrieveSummaryInfo 
    // We got a valid ClaimLoading value, display the summary 
    Ok(html.claimloading.summary(claimLoading, Json.prettyPrint(resultSummary))) 
    } 

) 
} 

从按钮点击Jquery的通话>>>>

/views/claimloading/form.scala.html

<input type="button" class="btn primary" id="claimsLoadButton" value="Invoke Claim Loading"> 

Jquery的形式。 scala下索赔是>>>>

<script type="text/javascript" xmlns="http://www.w3.org/1999/html"> 

$(document).ready(function() { 
    $("#claimsLoadButton").click(function() { 
     createLoadingModal(); 
     showLoader(true); 
     $.ajax({ 
      url: "/claimLoading", 
      type: "POST", 
      data: $("#claimLoadingForm").serialize(), // serializes the form's elements. 
      dataType:"json", 
      success: function (data) { 
       showLoader(false); 
      }, 
      error: function (jqXHR, textStatus, errorThrown) { 
      }, 
      complete: function (data) { 

       submitClaimsLoading(); 
      } 
     }); 
    }); 
}); 

function submitClaimsLoading() 
{ 
    $("#claimLoadingForm").submit(); 
    showLoader(false); 
} 

</script> 

回答

0

使用event.preventDefault();停止按钮单击提交表单。

$(document).ready(function (event) { 
    event.preventDefault(); //add this here 
    $("#claimsLoadButton").click(function() { 
     createLoadingModal(); 
     showLoader(true); 
     $.ajax({ 
      url: "/claimLoading", 
      type: "POST", 
      data: $("#claimLoadingForm").serialize(), // serializes the form's elements. 
      dataType: "json", 
      success: function (data) { 
       showLoader(false); 
      }, 
      error: function (jqXHR, textStatus, errorThrown) {}, 
      complete: function (data) { 

       submitClaimsLoading(); 
      } 
     }); 
    }); 
});