如何清除文件输入

时间:2012-04-09 23:58:17

标签: javascript jquery

如果仔细查看此代码,它会为每个附加行附加一个文件输入。现在每个文件输入都有一个“清除文件”按钮,它可以删除文件输入中的任何内容。问题是它没有清除文件输入中的任何内容。为什么这个以及如何解决?

JavaScript的:

var sourceImageForm; 

function insertQuestion(form) {   
    var $tbody = $('#qandatbl > tbody'); 
    var $tr = $("<tr class='optionAndAnswer' align='center'></tr>");
    var $image = $("<td class='image'></td>"); 

    var $fileImage = $("<form action='imageupload.php' method='post' enctype='multipart/form-data' target='upload_target' onsubmit='startImageUpload(this);' class='imageuploadform' >" + 
        "<p class='imagef1_upload_process' align='center'>Loading...<br/><img src='Images/loader.gif' /><br/></p><p class='imagef1_upload_form' align='center'><br/><label>" + 
        "Image File: <input name='fileImage' type='file' class='fileImage' /></label><br/><label class='imagelbl'>" + 
        "(jpg, jpeg, pjpeg, gif, png, tif)</label><br/><br/><label>" + 
        "<input type='submit' name='submitBtn' class='sbtn' value='Upload' /></label>" + 
        "<label><input type='button' name='imageClear' class='imageClear' value='Clear File'/></label>" +
        "</p> <iframe class='upload_target' name='upload_target' src='#' style='wclassth:0;height:0;border:0px;solclass #fff;'></iframe></form>");      

    $image.append($fileImage);
    $tr.append($image);  
    $tbody.append($tr); 

    $(".imageClear").click(function(event){
        event.preventDefault();
        $(this).parent().find(".fileImage").replaceWith("<input type='file' class='fileImage' name='fileImage' />");
    });

}

function startImageUpload(imageuploadform){
    $(imageuploadform).find('.imagef1_upload_process').css('visibility','visible');
    $(imageuploadform).find('.imagef1_upload_form').css('visibility','hidden');
    sourceImageForm = imageuploadform;
    return true;
}

function stopImageUpload(success){
    var result = '';
    if (success == 1) {
        result = '<span class="msg">The file was uploaded successfully!<\/span><br/><br/>';
    } else {
        result = '<span class="emsg">There was an error during file upload!<\/span><br/><br/>';
    }
    $(sourceImageForm).find('.imagef1_upload_process').css('visibility','hidden');
    $(sourceImageForm).find('.imagef1_upload_form').html(result + '<label>Image File: <input name="fileImage" type="file"/></label><br/><label>(jpg, jpeg, pjpeg, gif, png, tif)</label><br/><br/><label><input type="submit" name="submitBtn" class="sbtn" value="Upload" /></label><label><input type="button" name="imageClear" class="imageClear" value="Clear File"/></label>');
    $(sourceImageForm).find('.imagef1_upload_form').css('visibility','visible');     
    return true;   
}

2 个答案:

答案 0 :(得分:0)

您的一般策略似乎听起来 - 通过将其替换为另一个输入元素来“清除”类型文件的输入。我不知道为什么它不起作用,这是一个简单的POJS版本可以工作:

<form>
  <input type="file" name="fileName">
  <input type="button" onclick="clearFileName(this);" value="Clear file input">
</form>

<script type="text/javascript">

function clearFileName(el) {
  var input = el.form.fileName;
  input.parentNode.replaceChild(input.cloneNode(false), input);
}

</script>

答案 1 :(得分:0)

我写了一个清除文件输入的函数。它适用于所有浏览器。

演示: http://jsbin.com/muhipoye/1/

function clearInputFile(f){
    if(f.value){
        try{
            f.value = ''; //for IE11, latest Chrome/Firefox/Opera...
        }catch(err){
        }
        if(f.value){ //for IE5 ~ IE10
            var form = document.createElement('form'), ref = f.nextSibling;
            form.appendChild(f);
            form.reset();
            ref.parentNode.insertBefore(f,ref);
        }
    }
}

对于您的代码,您应该修改如下:

    function clearInputFile(f){
        if(f.value){
            try{
                f.value = '';
            }catch(err){
            }
            if(f.value){
                var form = document.createElement('form'), ref = f.nextSibling;
                form.appendChild(f);
                form.reset();
                ref.parentNode.insertBefore(f,ref);
            }
        }
    }
    $(".imageClear").click(function(){
        clearInputFile($(this).closest('form').find('input[type="file"]')[0]);
    });