如何在$(document).ready(function(e){

时间:2019-07-09 18:18:19

标签: jquery

我试图使用AJAX上传PDF文档,并使用文件名和“ app_id”引用更新数据库,但不知道如何从代码中获取额外的变量。

这是jquery代码:

我尝试了几种不同的方法来将数据获取到查询$(document).ready(function(e)代码)中,但它始终以“未定义”的形式返回。

$(document).ready(function(e){
$("#fupForm").change('submit', function(e){
    e.preventDefault();
    var creditapp_column = $('#creditapp_column').val();
    $.ajax({
        type: 'POST',
        url: 'upload1.php?column='+creditapp_column+'&app_id=123',
        data: new FormData(this),
        contentType: false,
        cache: false,
        processData:false,
        beforeSend: function(){
            $('.submitBtn').attr("disabled","disabled");
            $('#fupForm').css("opacity",".5");
        },
        success: function(msg){
            $('.statusMsg').html('');
            if(msg == 'ok'){
                $('#fupForm')[0].reset();
                $('.statusMsg').html('<span style="font-size:18px;color:#34A853">Form data submitted successfully.</span>');
            }else{
                $('.statusMsg').html('<span style="font-size:18px;color:#EA4335">Some problem occurred, please try again.</span>');
            }
            $('#fupForm').css("opacity","");
            $(".submitBtn").removeAttr("disabled");
        }
    });
});

//file type validation
$("#file").change(function() {
    var file = this.files[0];
    var imagefile = file.type;
    var match= ["image/jpeg","image/png","image/jpg"];
    if(!((imagefile==match[0]) || (imagefile==match[1]) || (imagefile==match[2]))){
        alert('Please select a valid image file (JPEG/JPG/PNG).');
        $("#file").val('');
        return false;
    }
});

});

我希望将以下其他变量添加到文件上传中:name =“ creditapp_supplied” creditapp_id ='。$ results [$ k] [“ app_id”]。'

1 个答案:

答案 0 :(得分:0)

编辑:-我被要求提供一种解决方案,用于在文件被选中时而不是在用户单击“提交”按钮时发布该文件。这就是您的做法。

//Store this globally
const AcceptedFileTypes = ["image/jpeg","image/png","image/jpg"];
//file type validation
$("#file").change(function() {
    var file = this.files[0];
    if(!AcceptedFileTypes.includes(file.type)){
        alert('Please select a valid image file (JPEG/JPG/PNG).');
        $("#file").val('');
        //You don't really want to return false, that's obtrusive.
    }
    else {
        $.ajax({
            type: 'POST',
            url: 'upload1.php?column='+creditapp_column+'&app_id=123',
            data: new FormData(this),
            contentType: false,
            cache: false,
            beforeSend: function(){
                //You might also want to show a preloader of some kind.
                $('.submitBtn').attr("disabled","disabled");
                $('#fupForm').css("opacity",".5");
            },
        }).done(function(data){
            //Do something after the success response
        }).fail(function(error){
            //Do something id the request fails.
        });
    }
});

将提交事件处理程序添加到表单的那一行中存在一个错误。更改它。

$("#fupForm").change('submit',function(e){//Your code});

$("#fupForm").on('submit',function(e){//Your code});

.change('event name', [handler callback])是不正确的语法,.change使用提供的处理程序回调将表单的change事件(不应绑定到该位置)绑定。您要寻找的是.submit([handler callback]).on("submit", [handler callback])