ajaxupload el未定义

时间:2013-03-08 07:19:31

标签: ajax jquery

我试图使用ajax上传图片。这是我的代码

$(function () {
    var btnUpload = $('#post_pd_thumnail');


    if ($("#id").val()) var post_id = $("#id").val();
    else var post_id = 0;

    new AjaxUpload(btnUpload, {
        action: site_root_domain + "/product/upload_image/" + post_id,
        name: 'file_upload',
        onSubmit: function (file, ext) {
            if ($('#post_pd_thumnail').val() != '') {
                if (!(ext && /^(jpg|png|jpeg|gif)$/.test(ext))) {
                    jAlert(lang_error_upload_avatar, lang_alert_notice);
                    return false;
                }
            }

            $('#preview').html('<img style="margin-left:45px;margin-top:45px;" border="0" width="16" height="11" src="' + site_root_domain + '/templates/images/icons/loading_2.gif" />');
        },
        onComplete: function (file, response) {

            if (!response) {
                jAlert(lang_error_upload_avatar, lang_alert_notice);
            } else {
                img_upload = 1;
                $('#preview').html(response);
            }

            return;
        }
    });

});

我的HTML是:

<div id="preview">{$preview}</div>
<div class="fileupload">
    <input type="file" name="post_pd_thumnail" id="post_pd_thumnail" value="" />
</div>
<input type="hidden" name="id" id="id" value="{$data['product']['post_id']}" />

我上传图片“el未定义”时出现此错误且功能无法正常工作,任何人都可以帮我解决这个问题

2 个答案:

答案 0 :(得分:0)

尝试将file element更改为button,例如

<div class="fileupload">
    <input type="button" name="post_pd_thumnail" id="post_pd_thumnail" value="" />
</div>

答案 1 :(得分:0)

这是解决方案。

* Attaches event to a dom element
 */
function addEvent(el, type, fn){

    // 610 BUG
    if (el == undefined) return;

    if (w.addEventListener){
        el.addEventListener(type, fn, false);
    } else if (w.attachEvent){
        var f = function(){
          fn.call(el, w.event);
        };          
        el.attachEvent('on' + type, f)
    }
}