我是否需要更改JavaScript代码以包含数值?

时间:2012-09-24 16:23:22

标签: javascript jquery

我有一个ajax文件上传器插件(Max's File Uploader),我在其中复制并粘贴代码并进行了一些更改。

现在下面是一个jQuery代码,它测量文件是成功,不成功还是取消:

function stopImageUpload(success, imagefilename){

  var result = '';
  imagecounter++;

  var replaceForm = function(result) { 
    $(sourceImageForm).find('.imagef1_upload_form').html(result + '<label>Image File: <input name="fileImage" class="fileImage" type="file"/></label><br/><br/><label><input type="submit" name="submitImageBtn" class="sbtnimage" value="Upload" /></label><label><input type="button" name="imageClear" class="imageClear" value="Clear File"/></label>');
  } 

  var updateForm = function(result) { 
    $(sourceImageForm).find('.imagef1_upload_form').find('.display_content_image').html(result);
  } 

  var displayInfo;

  if (success === 1){
    result = '<span class="imagemsg'+imagecounter+'">The file was uploaded successfully</span><br/><br/>';      
    $('.listImage').eq(window.lastUploadImageIndex).append('<div>' + htmlEncode(imagefilename) + '<button type="button" class="deletefileimage" image_file_name="' + imagefilename + '">Remove</button><br/><hr/></div>'); 
    displayInfo = replaceForm;      
  }
  else if (success === 2){
    result = '<span class="imagemsg'+imagecounter+'"> The file upload was canceled</span><br/><br/>';
    displayInfo = updateForm;    
  }
  else {
    result = '<span class="imagemsg'+imagecounter+'">There was an error during file upload</span><br/><br/>';
    displayInfo = updateForm;  
  }
  displayInfo(result); 

)};

但我有一个JavaScript函数,它在PHP脚本中输出以下内容:

<script language="javascript" type="text/javascript">
    window.top.stopImageUpload(<?php echo $result ? 'true' : 'false'; ?>, '<?php echo $_FILES['fileImage']['name'] ?>');
</script>

但我的问题是,如果上面的“真实”或“假”,它应该是1:2,因为我使用数值而不是字符串值来衡量文件的成功吗?

1 个答案:

答案 0 :(得分:1)

您可以使用1和2来代替使用truefalse

  <script language="javascript" type="text/javascript">window.top.stopImageUpload(<?php echo $result ? 1 : 2; ?>, '<?php echo $_FILES['fileImage']['name'] ?>');</script>