jQuery Form发送额外的post变量AJAX

时间:2011-08-28 05:19:20

标签: jquery ajax forms variables post

您好我正在将jFormer库集成到Wordpress中,并在Ajax调用WordPress时遇到麻烦。我在下面的WordPress插件中注册了我的Ajax handers:

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,下面摘录我认为请求是由表单提出的。非常感谢,克里斯

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();
    }
},

1 个答案:

答案 0 :(得分:0)

之后:

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);