2011-01-07 121 views

回答

0

你必須有實際的上傳處理PHP頁面響應上傳文件的文件名。

在fengcool的AJAX,它startUpload提供()函數:

var response = $(myFrame.contentWindow.document.body).text(); 

您使用的「響應」變量,無論你需要把文件名。

它是作爲變量「圖像」實際上傳遞給addUpload()函數,你可以通過修改也填充您的文本框中,大約是這樣的:

的document.getElementById(「圖像」)值=圖片

但是,您應該以不太通用的方式命名您的<input>以避免混淆。

UPDATE,做什麼:

1)命名的文本框在一個更獨特的方式,例如:

<input id="uploaded_image_name" type="text" value="" /> 

//另請注意,我用「身份證」,而不是「名稱」爲了能夠使用JavaScript功能的getElementById()

2)使用fengcool的Ajax,並更改功能addUpload()這樣的:

function addUpload(id,img){ 
    var div = $(document.createElement('div')).attr('id',id); 

    //add uploaded image 
    div.html("<img src='"+img+"'><br />"); 
    document.getElementById("uploaded_image_name").value=img 

    //add text box 
    var fileName = img.substring(img.lastIndexOf("/")+1); 
    var txtbx = $(document.createElement('input')).attr('name','img[]').attr('type','text').val(fileName); 
    /* you may want to change textbox to a hidden field in production */ 
    //var txtbx = $(document.createElement('input')).attr('name','img[]').attr('type','hidden').val(fileName); 
    txtbx.appendTo(div); 


    //add remove thumbnail link 
    var rem = $(document.createElement('a')) 
           .attr('alt',id) 
           .attr('href','javascript:;') 
           .text("Remove").click(removeUpload);  
    rem.appendTo(div); 

    //add to the page 
    div.appendTo("#uploaded_thumb"); 
} 

請注意,唯一的變化是在函數中添加了第4個命令。

+0

感謝您的回答,並原諒我在這裏是一個完整的新手,但通過「變量圖像」,你的意思是這個函數addUpload(id,img)`?所以我的文本框必須與腳本創建的文本框具有相同的ID?我是不是把document.getElementById部分? – KilgoreTrout 2011-01-07 16:55:27