恢复旧的提交功能javascript

时间:2013-01-21 09:40:16

标签: javascript jquery forms form-submit

你有一个包含输入数据的表单。像

<form id = 'myform'>
...
            <td><input type="checkbox" name="supplier_aid" value="on" checked disabled >{$output.t_artikelnr}</td>
            <td><input type="checkbox" name="descshort" value="on">{$output.t_kurztext}</td>
            <td><input type="checkbox" name="buyer_aid" value="on">{$output.t_sap_materialnr}</td>
...
</form>

on document ready我用我的函数替换默认的提交函数(唯一的方法,遗留代码再次出现......)

document.myform.submit = function (){
 //do some stuff..

//and eventually

return true;
}

但现在没有表单提交发生......有谁知道如何恢复提交行为?

这是整个js

<script type="text/javascript">
       $(document).ready(function(){


        document.articlelistform.progress = {};     
        document.articlelistform.progress.update = function(){
            var variables = document.articlelistform.progress;
            var state = variables.data.done / variables.data.togo;    
            if(document.articlelistform.progress.lock){
                 variables.bar.updateProgress(state,'{/literal}{$output.m_queue_progress_message_status}{literal}')
                 return;
            }


            if(typeof(variables.togo) =='undefined'){
                variables.togo = 5;
                }
            switch(variables.data.state){
                case 'inQue':

                        variables.bar.updateProgress((5 - variables.togo) / 5,'{/literal}{$output.m_queue_progress_message_gathering}{literal}');   
                        variables.togo--;
                        if(variables.togo <= 0) variables.togo = 5;
                        document.articlelistform.submit(variables.data);

                    break;
                case 'inProcess':
                    if(variables.togo <= 0 || variables.data.update){
                        variables.togo = 5;
                        variables.bar.updateProgress(state,'{/literal}{$output.m_queue_progress_message_genarating}{literal} '+ variables.data.done + '/' + variables.data.togo);   
                        document.articlelistform.submit(variables.data);

                    }else variables.togo--;

                    break;
                case 'downloadDone': 
                        variables.bar.updateProgress(1,'{/literal}{$output.m_queue_progress_message_download}{literal}!');   
                            if(window.location != 'failed'){
                                window.location = variables.data.location;
                                clearInterval(variables.ticking);
                                delete variables.ticking;
                                variables.togo = 5;
                                variables.state = 'new';
                            }else{
                                document.articlelistform.progress.bar.updateProgress(1,'{/literal}{$output.m_queue_progress_message_networkerr}{literal}!');
                            }
                       break;
                 case 'error' :
                     clearInterval(variables.ticking);
                     delete variables.ticking;
                     variables.togo = 5;
                     variables.state = 'error';
                     document.articlelistform.progress.bar.updateProgress(1,'{/literal}{$output.m_queue_progress_message_server_error}{literal}');
                     document.articlelistform.submit(variables.data);
                     break;

            }
        }

        document.articlelistform.submit = function(data){
            var settings = document.articlelistform.progress;
            settings.lock  = true;

            if(typeof(data) == 'undefined') data = {state:'new'};

            if(typeof(settings.data) != 'undefined')
            if(settings.data.state == 'downloadDone'){
               window.location = settings.data.location; 
               return false;
            }
            if(typeof(data) == 'object')
                if(data.state == 'error') document.articlelistform.submit()

            $('#formSettings').val(JSON.stringify(data));       
            $.ajax({
                type: "POST",
                url: 'ajax-backend/downloadCatalogueProgress.php',
                data:   $(this).serialize() ,

                success: function(data){

                    settings.lock  = false;
                    if(!data.error){

                        if((data.state == 'inQue' && typeof(settings.bar) == 'undefined')||data.progress){
                            delete data.progress;
                            $('#catalogMessageHolder').html('');
                            settings.bar = new Ext.ProgressBar({
                                renderTo: 'catalogMessageHolder',
                                value: 0.1,
                                width: 200,
                                maxValue:60,
                                minValue: 0,
                                text : '{/literal}{$output.m_queue_progress_message_genarating}{literal}.'

                            });
                            settings.data  = data;     
                            document.articlelistform.progress.ticking  = setInterval(settings.update, 1000, data, true);
                        }else{
                            settings.data  = data;
                        }
                        $('#cataloLoaderHolder').show();

                    }else{
                    //@todo: implement a warning message
                    }

                },
                failure: function(){
                 document.articlelistform.progress.bar.updateProgress(1,'{/literal}{$output.m_queue_progress_message_networkerr}{literal}!');
                } 
            });

            return true; 
        };
    });

    </script>

3 个答案:

答案 0 :(得分:1)

我设法得到我的主管的许可,以改变该死的事。

我明白为什么他要求我改变它...它在20个地方被硬编码......改变了所有。  刚刚使用jquery调用更改了js调用,并且所有操作都应该正常工作。不知道为什么它早点工作。但是这是一个叫做函数的点击

> <td class="blue" background="gfx/buttonb.gif" align="center"
> valign="center"><nobr><a  href="javascript:void(0)" class="blue"
> onclick="if (!(document.articlelistform.catalogpartner3200.checked ||
> document.articlelistform.catalogpartner3582.checked ||
> document.articlelistform.catalogpartner5320.checked ||
> document.articlelistform.catalogpartner5109.checked ||
> document.articlelistform.catalogpartner4774.checked ||
> document.articlelistform.catalogpartner3271.checked ||
> document.articlelistform.catalogpartner3526.checked ||
> document.articlelistform.catalogpartner5544.checked ||
> document.articlelistform.catalogpartner3494.checked ||
> document.articlelistform.catalogpartner3416.checked ||
> document.articlelistform.catalogpartner4439.checked ||
> document.articlelistform.catalogpartner5328.checked ||
> document.articlelistform.catalogpartner5329.checked ||
> document.articlelistform.catalogpartner3267.checked ||
> document.articlelistform.catalogpartner3512.checked ||
> document.articlelistform.catalogpartner2848.checked ||
> document.articlelistform.catalogpartner3417.checked ||
> document.articlelistform.catalogpartner3491.checked ||
> document.articlelistform.catalogpartner3492.checked ||
> document.articlelistform.catalogpartner5554.checked ||
> document.articlelistform.catalogpartner5591.checked ||
> document.articlelistform.catalogpartner5611.checked ||
> document.articlelistform.catalogpartner3517.checked ||
> document.articlelistform.catalogpartner5305.checked ||
> document.articlelistform.catalogpartner3532.checked ||
> document.articlelistform.catalogpartner5077.checked ||
> document.articlelistform.catalogpartner3581.checked ||
> document.articlelistform.catalogpartner3507.checked ||
> document.articlelistform.catalogpartner3579.checked ||
> document.articlelistform.catalogpartner3489.checked ||
> document.articlelistform.catalogpartner3490.checked ||
> document.articlelistform.catalogpartner5306.checked ||
> document.articlelistform.catalogpartner5598.checked ||
> document.articlelistform.catalogpartner5191.checked ||
> document.articlelistform.catalogpartner3305.checked ||
> document.articlelistform.catalogpartner3578.checked ||
> document.articlelistform.catalogpartner3580.checked ||
> document.articlelistform.catalogpartner4934.checked ||
> document.articlelistform.catalogpartner4437.checked ||
> document.articlelistform.catalogpartner3568.checked ||
> document.articlelistform.catalogpartner4079.checked ||
> document.articlelistform.catalogpartner3723.checked ||
> document.articlelistform.catalogpartner5321.checked ||
> document.articlelistform.catalogpartner3569.checked)) {alert('Sie
> müssen mind. 1 Katalog auswählen');  } else { if
> (!(document.articlelistform.supplier_aid.checked ||
> document.articlelistform.descshort.checked ||
> document.articlelistform.buyer_aid.checked ||
> document.articlelistform.desclong.checked ||
> document.articlelistform.keywords.checked ||
> document.articlelistform.manufacturer_name.checked ||
> document.articlelistform.delivery_time.checked ||
> document.articlelistform.remarks.checked ||
> document.articlelistform.reference_feature_group_id.checked ||
> document.articlelistform.order_unit.checked ||
> document.articlelistform.content_unit.checked ||
> document.articlelistform.quantity_min.checked ||
> document.articlelistform.quantity_interval.checked ||
> document.articlelistform.price_amount.checked ||
> document.articlelistform.special_treatment_class.checked ||
> document.articlelistform.mimeinfo.checked ||
> document.articlelistform.sicherheitsdatenblatt.checked )) {alert('Sie
> müssen mind. 1 Auswahlkriterium wählen!');  } else {
> $('#catalogDownloadForm').submit(); } } return false;" onMouseOver=
> "window.status='Als CSV-Datei downloaden'; return true;"
> onMouseOut="window.status=''; return true;">&nbsp;Als CSV-Datei
> downloaden&nbsp;</a></td>

代码很疯狂....我刚刚从document.articlelistform.submit()更改为$('#catalogDownloadForm').submit()并且所有工作都应该如此......不知道为什么它会失败。

答案 1 :(得分:0)

document.myform.submit = function (event) {
    // init event if undefined
    if (!event) var event = window.event;
    if (doNotSubmit) {
        event.preventDefault();
        // form won't be submitted
    } else {
        // will automatically submit, no need to return anything
    }
}

修改

显然,你正在使用jQuery。然后,另一个解决方案可以工作:

$("#myForm").on("submit", function() {
    if (doNotSubmit) {
        return false;
        // form won't be submitted
    } else {
        return true;
        // form will be submitted
    }
});

答案 2 :(得分:0)

您尚未声明onsubmit事件处理程序,您已重新声明提交自己!!!

所以要么,你真的想做:

document.myform.onsubmit = function(){
  // do some stuff...
  // and eventually
  return true;
};

看起来你做了,否则就没有必要返回任何内容,因为document.myform.submit没有。

如果这是故意的(eek!),在重新定义document.myform.submit之前,您需要存储它:

document.myform.originalSubmit = document.myform.submit;
document.myform.submit = function (){
  //do some stuff..
  //and eventually
  document.myform.originalSubmit();
};

由于这会绕过您与表单关联的所有事件处理程序,我不会看到您真正想要的是什么,因为它不能很好地与其他代码一起使用。