jQuery表单发送附加的post变量AJAX



嗨,我正在将jFormer库集成到Wordpress中,但是在使用Ajax调用Wordpress时遇到了一个问题。我已经在下面的WordPress插件中注册了Ajax处理程序:

add_action('wp_ajax_nopriv_jFormerForWp', 'JFormerForWP::AjaxHandler');
add_action('wp_ajax_jFormerForWp', 'JFormerForWP::AjaxHandler');

实际上我需要做的是在POST请求中发送一个名为action => jFormerForWp的变量。

问题是我的jQuery技能有限,我已经问过开发人员,他们可能会在他们有能力的时候回复我,但我想我会把这个问题开放给一般的jQuery社区,希望他们能帮助我。

所以为了确认,我需要修改jQuery代码来发送action = jFormerForWp.

代码在这里http://www.jformer.com/download/jFormer-dev.zip与下面的摘录,我认为请求是由表单。非常感谢,Chris

submitForm: function(event) {
    var self = this;
    // Use a temporary form targeted to the iframe to submit the results
    var formClone = this.form.clone(false);
    formClone.attr('id', formClone.attr('id')+'-clone');
    formClone.attr('style', 'display: none;');
    formClone.empty();
    formClone.appendTo($(this.form).parent());
    // Wrap all of the form responses into an object based on the component jFormComponentType
    var formData = $('<input type="hidden" name="jFormer" />').attr('value', encodeURI(jFormerUtility.jsonEncode(this.getData()))); // Set all non-file values in one form object
    var formIdentifier = $('<input type="hidden" name="jFormerId" value="'+this.id+'" />');
    formClone.append(formData);
    formClone.append(formIdentifier);

    this.form.find('input:file').each(function(index, fileInput) {
        if($(fileInput).val() != '') {
            // grab the IDs needed to pass
            var sectionId = $(fileInput).closest('.jFormSection').attr('id');
            var pageId = $(fileInput).closest('.jFormPage').attr('id');
            //var fileInput = $(fileInput).clone()
            // do find out the section instance index
            if($(fileInput).attr('id').match(/-section[0-9]+/)){
                var sectionInstance = null;
                var section = $(fileInput).closest('.jFormSection');
                // grab the base id of the section to find all sister sections
                var sectionBaseId = section.attr('id').replace(/-section[0-9]+/, '') ;
                sectionId = sectionId.replace(/-section[0-9]+/, '');
                // Find out which instance it is
                section.closest('.jFormPage').find('div[id*='+sectionBaseId+']').each(function(index, fileSection){
                    if(section.attr('id') == $(fileSection).attr('id')){
                        sectionInstance = index + 1;
                        return false;
                    }
                    return true;
                });
                 fileInput.attr('name', fileInput.attr('name').replace(/-section[0-9]+/, '-section'+sectionInstance));
            }
            // do find out the component instance index
            if($(fileInput).attr('id').match(/-instance[0-9]+/)){
                // grab the base id of the component to find all sister components
                var baseId = $(fileInput).attr('id').replace(/-instance[0-9]+/, '')
                var instance = null;
                // Find out which instance it is
                $(fileInput).closest('.jFormSection').find('input[id*='+baseId+']').each(function(index, fileComponent){
                    if($(fileComponent).attr('id') == $(fileInput).attr('id')){
                        instance = index + 1;
                        return false;
                    }
                    return true;
                });
                 fileInput.attr('name', $(fileInput).attr('name').replace(/-instance[0-9]+/, '-instance'+instance));
            }
            $(fileInput).attr('name', $(fileInput).attr('name')+':'+pageId+':'+sectionId);
            $(fileInput).appendTo(formClone);
        }
    });
    // Submit the form
    formClone.submit();
    formClone.remove(); // Ninja vanish!
    // Find the submit button and the submit response
    if(!this.options.debugMode){
        this.controlNextButton.text(this.options.submitProcessingButtonText).attr('disabled', 'disabled');
    }
    else {
        this.form.find('iframe:hidden').show();
    }
},

之后:

var formData = $('<input type="hidden" name="jFormer" />').attr('value', encodeURI(jFormerUtility.jsonEncode(this.getData()))); // Set all non-file values in one form object
var formIdentifier = $('<input type="hidden" name="jFormerId" value="'+this.id+'" />');
formClone.append(formData);
formClone.append(formIdentifier);

附加:

var formExtra = $('<input type="hidden" name="action" value="jFormerForWp" />');
formClone.append(formExtra);

最新更新