2012-04-04 49 views
0

一個功能我已經聲明瞭一個函數來顯示在jQuery的調用Javascript中

<script type="text/javascript"> 
function showDialog(str,strtitle) 
{ 
if(!strtitle) strtitle='Error message'; 
$('#dialog').dialog('destroy'); 
$('#dialog').show(); 
$('#dialog').html(str); 
$("#dialog").dialog({ 
    resizable: false, 
    width: 400, 
    color: '#BF5E04', 
     open: function() { 
        $(this).parents(".ui-dialog:first").find(".ui-dialog 
           titlebar").addClass("ui-state-error");}, 

    buttons: {"Ok": function() { 
    $(this).dialog("close"); }}, 

    overlay: { opacity: 0.2, background: "cyan" },title:strtitle});} 
    </script> 

一個對話框,我調用這個函數,在另一個javascript代碼:

<script type="text/javascript"> 
    var myFile = document.getElementById('myfile'); 
    //binds to onchange event of the input field 
    myFile.addEventListener('change', function() { 
    //this.files[0].size gets the size of your file. 
    var size = this.files[0].size; 
    document.write(showDialog('size','File Size Exceeds')); 
     }); 
    </script> 

當我執行函數,它寫入Undefined,爲什麼對話框沒有顯示。第一個功能是在頭部,第二個在身體部分。

+7

它的寫入定義是因爲函數showDialog沒有返回任何東西。 document.write()不是必需的,只需調用showDialog()。 – Ivan 2012-04-04 15:47:46

+0

您是否試過通過類似螢火蟲的方式運行它?並通過步進,順便說一句:你的第一個節的第13行 - 是該行打破存在於真正的代碼,還是你把它放在使它適合更加整齊? – 2012-04-04 15:49:23

+0

@Ivan是正確的,只是一個加法:直到頁面加載您可能要推遲調用'showDialog':'$(的ShowDialog);'。 – DCoder 2012-04-04 15:49:56

回答

3

document.write()在寫什麼是showDialog()返回。由於該函數不返回任何內容,因此將寫入Undefined

執行document.write()是沒有必要的,只是簡單地調用的ShowDialog()。

+0

好的,但添加代碼後,你說什麼,它什麼都不做。 – 2012-04-04 15:56:40

+0

@Hanya Idrees:你什麼意思是「它什麼都不做」。這是說很少。它拋出一個錯誤? – Ivan 2012-04-04 15:57:55

+0

不,沒有錯誤,也沒有功能我的意思是說。 – 2012-04-04 15:59:34

0

你是不是在ShowDialog的()函數返回的任何值。

0

剛剛擺脫document.write()showDialog()沒有任何返回,因此未定義的錯誤。

<script type="text/javascript"> 

    var myFile = document.getElementById('myfile');  
    //binds to onchange event of the input field  
    myFile.addEventListener('change', function() {  
    //this.files[0].size gets the size of your file.  
    var size = this.files[0].size;  
    showDialog('size','File Size Exceeds');   
    });  

</script>