首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

ajax下传时参数提交的有关问题

2013-01-07 
ajax上传时参数提交的问题我是下载的一个jQuery的Ajax上传插件。网页左侧是上传分类,我打算在服务端根据上

ajax上传时参数提交的问题
我是下载的一个jQuery的Ajax上传插件。网页左侧是上传分类,我打算在服务端根据上传分类建立不同的文件夹来保存文件,所以要传一个上传分类参数给后台。这些上传分类是从数据库读出,用repeater生成的。当点击其中一个分类时就给他设置一个样式,并且将这个分类的名字保存到一个变量uploadCatlog中。我可以取到分类并传给后台,但是只有第一次是正确的,以后的每次参数都不更新。其实在上传插件的onComplete回调函数中也用到了uploadCatlog,他的值已经更新了。我觉得好像这个上传插件只在第一次点击的时候实例化并将参数传给后台,所以以后值都是不变的。。应该怎么解决这个问题呢?求大神指点。。


<script type="text/javascript">
    $(function () {
        var uploadCatlog;

        //设置母版页导航栏的当前选中样式
        $("#menu .nav5").addClass("menu_active").siblings().removeClass("menu_active");
        //生成类别菜单样式和导航菜单
        $("#kllst li").click(function () {
            $(this).addClass("currentli").siblings().removeClass();
            uploadCatlog = $(this).text();
        });

        var btnUpload = $('#upload');
        var status = $('#status');
        btnUpload.click(function () {
 
            if (uploadCatlog == undefined) {
                status.text("必须先选择上传文件的类别!").addClass('error');
                return false;
            }

            new AjaxUpload(btnUpload, {
                action: 'handler/doUpload.ashx',
                name: 'uploadfile',
                onSubmit: function (file, ext) {
                    if (!(ext && /^(doc|docx|xls)$/.test(ext))) {
                        status.text('只支持WORD,EXCEL格式上传!').addClass('error');
                        return false;
                    }
                    status.text('正在上传,请稍候...');
                },
                data: { "catlog": uploadCatlog },
                onComplete: function (file, response) {
                    status.text('').removeClass('error');
                    if (response == "success") {



                        var fimgtype;
                        if (file.indexOf(".doc") != -1 || file.indexOf(".docx") != -1) {
                            fimgtype = "word";
                        }
                        if (file.indexOf(".xls") != -1) {
                            fimgtype = "excel";
                        }

                        $('<li></li>').appendTo('#files').html('<img src="images/' + fimgtype + '.png" alt="ajax下传时参数提交的有关问题" /><br />[' + uploadCatlog + ']' + file).addClass('success');
                    } else {
                        $('<li></li>').appendTo('#files').text(file).addClass('error');
                    }
                }
            });
        });
    });
</script>


[解决办法]
创建实例后就不会变了,可以在onSubmit回调函数中调用SetData方法设置动态参数

//您可以使用这些方法来配置AJAX的上传
var upload = new AjaxUpload(‘#div_id’,{action: ‘upload.php’});
//例如当用户选择了一些东西,设置一些参数
upload.setData({ "catlog": uploadCatlog });

热点排行