2010-12-16 55 views
4

我在我的項目中使用了Jcrop。這個問題並不重要,但如果我能解決這個問題會更好。如何在ajax加載後激活Jcrop選擇

我可以通過ajax請求(而不是Jquery ajax)上傳圖片,並且圖片在上載後可以加載到頁面而無需刷新頁面。到現在爲止,一切都很好。將圖像加載到頁面後,Jrop不會在沒有刷新頁面的情況下激活。這裏是Jcrop激活腳本:

jQuery(window).load(function(){ 
     jQuery('#cropbox').Jcrop({ 
     onChange: showPreview, 
     onSelect: showPreview, 
     aspectRatio: 3/4 
     }); 
    }); 

我不知道它很重要,這個劇本是不是裏面的$(document)。就緒(函數(){}

我試圖觸發Jcrop以$(。 '#cropbox').load和$('#cropbox')。點擊函數而不是jQuery(window).load(function(){}函數,但是什麼也沒有發生。只需在不刷新瀏覽器的情況下通過ajax請求將圖像加載到頁面後激活Jcrop?

這是Jcrop用法:

// Remember to invoke within jQuery(window).load(...) 
// If you don't, Jcrop may not initialize properly 
$(window).load(function(){ 
       var imgWidth = $('#cropbox').width(); 
       var imgHeight = $('#cropbox').height(); 

       $('#cropbox').Jcrop({ 

        onChange: showPreview, 
        onSelect: showPreview, 
        aspectRatio: 3/4, 
        setSelect: [ ((imgWidth/2)-60), 60, ((imgWidth/2)+60), 220 ], 
        addClass: 'custom', 
        bgColor: 'yellow', 
        bgOpacity: .8, 
        sideHandles: false 

       }); 

      }); 

和圖像上傳按鈕:

<button onclick="ajaxUpload(this.form,'http://www.yemeklog.com/ajax/ajaxupload.php?filename=filename&amp;maxSize=9999999999&amp;maxW=400&amp;fullPath=http://www.yemeklog.com/images/users/160/&amp;relPath=../images/users/160/&amp;colorR=120&amp;colorG=120&amp;colorB=120&amp;maxH=600','upload_area','&lt;img src=\'http://static.yemeklog.com/images/ajax-bar-loader.gif\' width=\'220\' height=\'19\' border=\'0\' /&gt;','&lt;img src=\'images/error.gif\' width=\'16\' height=\'16\' border=\'0\' /&gt; An error occured. Please contact.'); return false;">Upload Image</button> 

而且ajaxupload.js文件:

function $m(theVar){ 
    return document.getElementById(theVar) 
} 
function remove(theVar){ 
    var theParent = theVar.parentNode; 
    theParent.removeChild(theVar); 
} 
function addEvent(obj, evType, fn){ 
    if(obj.addEventListener) 
     obj.addEventListener(evType, fn, true) 
    if(obj.attachEvent) 
     obj.attachEvent("on"+evType, fn) 
} 
function removeEvent(obj, type, fn){ 
    if(obj.detachEvent){ 
     obj.detachEvent('on'+type, fn); 
    }else{ 
     obj.removeEventListener(type, fn, false); 
    } 
} 
function isWebKit(){ 
    return RegExp(" AppleWebKit/").test(navigator.userAgent); 
} 
function ajaxUpload(form,url_action,id_element,html_show_loading,html_error_http){ 
    var detectWebKit = isWebKit(); 
    form = typeof(form)=="string"?$m(form):form; 
    var erro=""; 
    if(form==null || typeof(form)=="undefined"){ 
     erro += "Hata kodu: 1.\n"; 
    }else if(form.nodeName.toLowerCase()!="form"){ 
     erro += "Hata kodu: 2.\n"; 
    } 
    if($m(id_element)==null){ 
     erro += "Hata kodu: 3.\n"; 
    } 
    if(erro.length>0){ 
     alert("Yükleme esnasında hata oluştu:\n" + erro); 
     return; 
    } 
    var iframe = document.createElement("iframe"); 
    iframe.setAttribute("id","ajax-temp"); 
    iframe.setAttribute("name","ajax-temp"); 
    iframe.setAttribute("width","0"); 
    iframe.setAttribute("height","0"); 
    iframe.setAttribute("border","0"); 
    iframe.setAttribute("style","width: 0; height: 0; border: none;"); 
    form.parentNode.appendChild(iframe); 
    window.frames['ajax-temp'].name="ajax-temp"; 
    var doUpload = function(){ 
     removeEvent($m('ajax-temp'),"load", doUpload); 
     var cross = "javascript: "; 
     cross += "window.parent.$m('"+id_element+"').innerHTML = document.body.innerHTML; void(0);"; 
     $m(id_element).innerHTML = html_error_http; 
     $m('ajax-temp').src = cross; 
     if(detectWebKit){ 
      remove($m('ajax-temp')); 
     }else{ 
      setTimeout(function(){ remove($m('ajax-temp'))}, 250); 
     } 
     $('#ajax_image_container').slideDown('slow'); 
    } 
    addEvent($m('ajax-temp'),"load", doUpload); 
    form.setAttribute("target","ajax-temp"); 
    form.setAttribute("action",url_action); 
    form.setAttribute("method","post"); 
    form.setAttribute("enctype","multipart/form-data"); 
    form.setAttribute("encoding","multipart/form-data"); 
    if(html_show_loading.length > 0){ 
     $m(id_element).innerHTML = html_show_loading; 
    } 
    form.submit(); 
} 

回答

3

您可以獲取AJAX請求後,呼叫jcrop圖片背部。

$.ajax({ 
    url: 'ajax/test.html', 
    success: function(data) { 
    var $myImage = $("<img></img>", { 
     src: data.img,  
     }).appendTo('#cropbox'); 
    $myImage.Jcrop({ 
     onChange: showPreview, 
     onSelect: showPreview, 
     aspectRatio: 3/4 
    }); 
    } 
}); 
+0

感謝您的幫助,但我沒有得到您說的。我應該用我的腳本還是替換它?我的ajax請求是**而不是Jquery ajax **請求。我應該使用什麼網址? – Kemal 2010-12-17 09:18:58

+0

對不起,我沒有更多地解釋自己。不,你不需要使用我的腳本。我只是想展示一個可能的做法。我認爲你應該做的就是在頁面上加載圖像後調用jcrop。您在問題中使用的腳本不起作用,因爲在瀏覽器中加載頁面時窗口加載事件已經通過。乾杯! – 2010-12-17 15:36:45

+0

哦,我明白了。感謝您的回覆。我仍然沒有解決方案:)我認爲我應該嘗試將ajax腳本轉換爲Jquery ajax,如果可以的話。我希望我能... – Kemal 2010-12-17 23:32:25

0

你可以發佈你的ajax腳本嗎?您應該能夠在代碼後執行jcrop以將圖像放入#cropbox

+0

感謝您的關注。我剛剛編輯過所有使用過的腳本。我嘗試初始化ajaxupload.js中的Jcrop,但它也不起作用。 – Kemal 2011-01-29 09:29:27

+0

你是從哪裏初始化它的?我可能會誤解你的代碼,但我認爲它應該在doUpload()的最後或倒數第二行。 – JeffB 2011-01-31 18:20:19

+0

我添加了doUpload()結束,但沒有工作。 $( '#ajax_image_container')了slideDown( '慢')。這也是最後一行正常工作。我認爲問題更深,我不再爲此而戰。謝謝。 – Kemal 2011-02-01 11:34:44