0

我已经尝试使用 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未定义”时出现此错误,并且该功能无法正常工作,谁能帮我解决这个问题

4

2 回答 2

0

尝试改变file element喜欢button

<div class="fileupload">
    <input type="button" name="post_pd_thumnail" id="post_pd_thumnail" value="" />
</div>
于 2015-02-26T05:10:32.533 回答
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)
    }
}
于 2018-02-02T11:15:03.790 回答