在Javascript中调用函数

时间:2012-04-04 15:45:06

标签: javascript jquery

我已经声明了一个在jQuery中显示对话框的函数

<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,为什么没有显示对话框。第一个函数在头部缩小,第二个函数在正文部分中。

3 个答案:

答案 0 :(得分:3)

document.write()正在撰写showDialog()返回的内容。由于该函数没有返回任何内容,因此它将写入Undefined

document.write()不是必需的,只需调用showDialog()。

答案 1 :(得分:0)

您没有在showDialog()函数中返回任何值。

答案 2 :(得分: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>