Ajaxsubmit,在beforeSubmit上链接事件

时间:2012-11-06 19:12:37

标签: jquery

我试图在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>

我遇到的问题是,一旦我不能拥有       return confirmUpdates(arr,$ form,options); 好好工作。它确实启动了模态窗口,但它不等待btnModSubmit单击。它提交表格,我看到我在这里?警告。

如果chkOptions中对stuff.cfm的ajax调用有效,现在去执行confirmUpdates。如果用户单击btnModSubmit,则返回true以执行showResponse。如果用户单击btnModClose,则返回false并且不执行showResponse。我被困在这一点上。

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

1 个答案:

答案 0 :(得分: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);

希望对某人有所帮助。 感谢