2012-04-04 47 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>