我试图在 beforeSubmit 中触发几个函数。首先,我想检查所选选项是否有效。其次,我想使用模式窗口并让用户确认更改。这是一些代码:
<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, $form, options); 好好工作。它会启动模态窗口,但不会等待 btnModSubmit 单击。它提交表单,我看到我在这里吗?警告。
如果在 chkOptions 中对 stuff.cfm 的 ajax 调用是有效的,那么现在进行确认更新。如果用户点击 btnModSubmit 则返回 true 以执行 showResponse。如果用户单击 btnModClose,则返回 false 并且不执行 showResponse。我被困在这一点上。
有没有办法解决这个问题?