替换并打印jquery formData值

Replace and Print jquery formData value

本文关键字:formData jquery 打印 替换      更新时间:2023-09-26

我想让jquery插件在ajax之前处理表单提交。

这是我的jquery脚本

;(function($,window,document,undefined){
     "use strict";
     $.modalLoad = function(element, options){
         var plugin = this;
         var $element = $(element),
             element = element,
             url = $element.attr('href'),
             target = $element.data('target');
         var defaults   = {
             form: $(this).serializeArray(),
        };
        plugin.init = function(context){
             plugin.settings = $.extend({},defaults, options);
             plugin.add_bindings();
             plugin.create_ajax(context);
    }
         plugin.create_ajax = function(context){
             $('form',context).addClass('modal-form');
             $('.modal-form',context).on('submit',function(e){
                 e.preventDefault();
                 plugin.post_data($(this),context);
             });
         }
         plugin.post_data = function(form,context){
             var loaded = false;
             var throbbed = false;
             var _fd = new FormData();
             var password = hex_sha512($('input[type="password"]',context).val());
             _fd.append('password',password);
             function checkComplete(){
                 if(loaded && throbbed){
                     $('.ajax-loader').remove();
                 }
             }
             function requestComplete(){
                 loaded = true;
                 checkComplete();
             }
             $.ajax({
                 url:form.attr('action'),
                 type: form.attr('method'),
                 data: _fd,
                 contentType: false,
                 cache: false,
                 processData: false,
                 success: function(data){
                    requestComplete();
                    console.log(data);
                 },
                 beforeSend: function(){
                     var loading = "<img src='images/loader.gif' class='ajax-loader'>";
                     $('.modal-footer',context).append(loading);
                     $('.ajax-loader').css({
                        height: '15px',
                        'vertical-align': 'middle',
                        margin: '0px 5px'
                     });
                     setTimeout(function(){
                         throbbed = true;
                         checkComplete();
                     },2000);
                 },
                 complete: requestComplete()
             });
             console.log(plugin.settings.form);
         }
         plugin.init();
     }
     $.fn.modalLoad = function(options){
         return this.each(function(){
             if(undefined == $(this).data('modalLoad')){
                 var plugin = new $.modalLoad(this, options);
                 $(this).data('modalLoad', plugin);
             }
         });
     }
 })(jQuery);

HTML

<div class="modal fade" id="login-modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="false">
    <div class="modal-dialog">
        <div class="modal-content">
        <form action="<?php echo 'http://'.base_url('authentication.php');?>" method="POST">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">Login</h4>
            </div>
            <div class="modal-body">
                <div class="modal-space">
                    <label class="email">
                        <input type="text" name="email" value="" placeholder="Email*" data-constraints="@Required @Email" id="regula-generated-387495">
                    </label>
                </div>
                <div class="modal-space">
                    <label class="password">
                        <input type="password" name="password" value="" placeholder="Password*" data-constraints="@Required @Email" id="regula-generated-387495">
                    </label>
                </div>
            </div>
            <div class="modal-footer">
                <input type="submit" class="btn btn-primary btn-1 btn-1__mod-1" value="LOGIN">
            </div>
        </form>
        </div>
    </div>
</div>

现在,我想用sha512加密密码字段,然后用ajax按照下面的说明发送。

实际上,我将表单数据序列化到数组中,并希望覆盖在defaults.form对象中设置的密码数组。

但即使是我也无法从defaults.form中获取数据,表单数据应该存储在.中

如果我在console.log中打印defaults.form,可能吗?每个人都能告诉我该修哪一部分吗?还请告诉我如何整理我的代码?

感谢提前

请确保在您的html代码中,所有输入都在标记表单中。否则,您将在defaults.form中从表单数据序列化中获得丢失的值。您能在这里显示您的html码吗?