2012-11-06 59 views
1

我想触发beforeSubmit中的几个函数。首先,我想检查选定的选项是否有效。其次,我想使用模式窗口并让用户确认更改。下面是一些代码:Ajaxsubmit,提前链接事件提交

<script type="text/javascript" language="javascript" src="scripts/jquery-latest.js"></script> 
<script type="text/javascript" language="javascript" src="scripts/jquery.form.js" ></script> 
<script type="text/javascript" language="javascript" src="scripts/jquery.simplemodal-1.4.3.js"></script> 
<link rel="stylesheet" type="text/css" href="css/modalWins.css" /> 
<script> 
$().ready(function() { 
$('#myForm').submit(function() {       
    var options = { 
     success: showResponse, 
     beforeSubmit: chkService, 
     dataType: "json", 
     url:'actCustomer.cfm?vw=saveSvc&acctNum=1' 
     }; 
    $('#myForm').ajaxSubmit(options); 
    return false; 
}); 

showResponse = function(responseText, statusText) { 
    alert('am i here? ' + responseText); 
    $.ajax({ 
     type: "post",  
     url: "actCustomer.cfm?vw=save", 
     cache: false,  
     success: function(result) { 
      alert(result); 
     }, 
     error: function(xmlHttpRequest, status, err) { 
      confirm('Error: '+err); 
      $('#errDiv').html(err); 
     } 
    }); 
} 

chkService = function(arr, $form, options) { 
    formData = 'service='+$('#svc').val(); 
    $.ajax({ 
     type: "post",  
     url: "actCustomer.cfm?vw=chkStuff", 
     data: formData, 
     cache: false,  
     success: function(result) { 
      result = $.trim(result); 
      if (result == 'Invalid') { 
       $('#errDiv').html('Invalid Selection').addClass('req'); 
       return false; 
      } else if (result == 'Valid') { 
       return confirmUpdates(arr, $form, options); 
      } else { 
       $('#errDiv').html(result); 
       return false; 
      } 
     }, 
     error: function(xmlHttpRequest, status, err) { 
      confirm('Error: '+err); 
      $('#errDiv').html(err); 
     } 
    }); 
} 


// should we continue with the updates? 
confirmUpdates = function(arr, $form, options) { 
    var dispMsg = 'some information here about updates.'; 
    $('#msg-header').html('Confirmation of Updates!'); 
    $('#msg-content').html(dispMsg).css('color','black'); 
    $('#msg-action').html('<div align="center" style="margin-top:30px;"><input type="button" name="btnModSubmit" id="btnModSubmit" class="button submit" value="Continue"/><input type="button" name="btnModClose" id="btnModClose" class="button close" value="Cancel" style="margin-left:30px"/></div>'); 
    $("#Msg").html('<table style="font-size:11px; color:#333; margin-left:35px;"><tr><td><img border="0" src="images/ajax-loader.gif"/></td><td style="font-weight:bold">Confirming updates...</td></table>'); 

    $('#basic-modal-content').modal({ minHeight:320, minWidth:455 }); 

    $('#btnModSubmit').click(function(){ 
     // continue with the updates. 
     alert('continue >>'); 
     return true; 
    }); 
    $('#btnModClose').click(function(){ 
     /* $.modal.close(); 
     $("#formMsg").hide(); 
     $('#formSub').show(); */ 
     window.location.reload(); 
    }); 
} 
}); 
</script> 
    <form name="myForm" id="myForm" action="" method="post"> 
service:<input type="text" name="svc" id="svc" value="1"> 
    <input type="submit" name="submit" id="submit" class="button submit" value="Proceed"/> 
</form> 

我遇到的问题是,一旦我不能有 回报confirmUpdates(ARR,$形式,选项); 正常工作。它会启动模式窗口,但不会等待btnModSubmit单击。它提交的表格,我看到我在这里?警告。

如果Ajax调用在chkOptions到stuff.cfm是有效的,现在转做confirmUpdates。如果用户点击btnModSubmit,则返回true以执行showResponse。如果用户点击btnModClose,则返回false并且不执行showResponse。我被困在这一点上。

有没有办法解决这个问题?

回答

2

我最终从ajaxSubmit会删除成功如下。

$('#myForm').submit(function() { 

    var options = { 
     beforeSubmit: function(arr, $form, options) { 
      chkService(arr, $form, options); 
      confirmUpdates(arr, $form, options); 
      return false; 
     }, 
     dataType: "json", 
     url:'actCustomer.cfm?vw=saveSvc&acctNum=1' 
     }; 
    $('#myForm').ajaxSubmit(options); 
    return false; 
}); 

chkService先运行。然后,它去确认更新。在那里,我有一个提交和取消按钮。如果他们点击提交,然后我将它们发送到另一个功能以保存表格:

saveForm(arr, $form, options); 

希望对某人有帮助。 感谢