Prevent changes after async form submission?

别等时光非礼了梦想. 提交于 2019-12-12 04:47:57

问题


So I have a function called submitEdit():

function submitEdit() {
    //var ffield = document.getElementById("form_eline");
    //ffield.submit();
    jQuery('#edit_line').css('visibility', 'hidden');

    var fieldName = jQuery('#form_eline input[name=efield]').val();
    var pageId = jQuery('#form_eline input[name=eid]').val();
    var fieldText = jQuery('#ve'+fieldName+'_'+pageId);
    var editBtn = fieldText.prev();
    var loading = document.createElement("img");
    loading.src = 'http://www.lookall.tv/html/res/img/loading.gif';
    loading.id = "loading";
    loading.width = 16;
    loading.height = 16;

    jQuery('#form_eline').ajaxSubmit({
        beforeSubmit: function() {
            editBtn.hide();
            fieldText.prepend(loading);
        },
        error: function(data) {
            jQuery('#edit_line').css('visibility', 'visible');
        },
        success: function(response) {
            fieldText.text(jQuery('#form_eline :text').fieldValue()[0]);
            editBtn.show();
            jQuery('img#loading').remove();

            return true;
        }
    });
}

Sadly this isn't my work and I'm building on an already existing website. So the problem is that the server is slow to respond. Therefor in those few seconds it takes the server to send a response someone could edit another field thus overwriting the current values. Is there some way to instantiate the variables so they don't overwrite?


回答1:


The variables won't be affected if the user will change the values of the HTML elements.

If you want the user to know the changes he will do won't affect because the request ha been sent already. just disable the elements.

beforeSubmit: function() {
            editBtn.hide();
            fieldText.prepend(loading);
            $('#form_eline input, select, textarea').prop('disabled', true);
        },
complete: function (){
            editBtn.show();
            $('#form_eline input, select, textarea').prop('disabled', false);
        }


来源:https://stackoverflow.com/questions/9092332/prevent-changes-after-async-form-submission

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!