最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to only clear file upload field? - Stack Overflow

programmeradmin2浏览0评论

Here is the JSfiddle: /

I want to clear only the file upload field when pressed on clear.

My HTML:

<h1>Upload image:</h1>
<input type="file" name="blog[opload]" id="blog_opload" class="imagec">
<input type="url" size="50" name="blog[url]" id="blog_url" class="imagec">
<div id="preview">
</div>

My Jquery:

$('input').change(function() {
    var el = $(this);
    if (this.value === "") {
        $('.imagec').prop('disabled', false);
        this.disabled = false;
        $('#preview').hide();
    } else {
        $('.imagec').prop('disabled', true);
        el.prop('disabled', false);
        alert(el.val());
        if (el.attr('type') == 'url') {
            $('#preview').show().html('<img src=' + '"' + el.val() + '"' + ' />');
        }
    }
});

function reset_html(id) {
    $('.' + id).html( $('.' + id).html() );
}

var imagec_index = 0;
    $('input[type=file]').each(function() {
    imagec_index++;
    $(this).wrap('<div id="imagec_' + imagec_index + '"></div>');
    $(this).after('<input type="button" value="Clear" onclick="reset_html(\'imagec_' + imagec_index + '\')" />');
});

Here is the JSfiddle: http://jsfiddle.net/buyC9/128/

I want to clear only the file upload field when pressed on clear.

My HTML:

<h1>Upload image:</h1>
<input type="file" name="blog[opload]" id="blog_opload" class="imagec">
<input type="url" size="50" name="blog[url]" id="blog_url" class="imagec">
<div id="preview">
</div>

My Jquery:

$('input').change(function() {
    var el = $(this);
    if (this.value === "") {
        $('.imagec').prop('disabled', false);
        this.disabled = false;
        $('#preview').hide();
    } else {
        $('.imagec').prop('disabled', true);
        el.prop('disabled', false);
        alert(el.val());
        if (el.attr('type') == 'url') {
            $('#preview').show().html('<img src=' + '"' + el.val() + '"' + ' />');
        }
    }
});

function reset_html(id) {
    $('.' + id).html( $('.' + id).html() );
}

var imagec_index = 0;
    $('input[type=file]').each(function() {
    imagec_index++;
    $(this).wrap('<div id="imagec_' + imagec_index + '"></div>');
    $(this).after('<input type="button" value="Clear" onclick="reset_html(\'imagec_' + imagec_index + '\')" />');
});
Share Improve this question edited Feb 2, 2012 at 19:37 Yahia 70.4k9 gold badges117 silver badges147 bronze badges asked Feb 2, 2012 at 19:34 Rails beginnerRails beginner 14.5k35 gold badges139 silver badges259 bronze badges 3
  • Why don't you use plugin for that? – gdoron Commented Feb 2, 2012 at 19:36
  • 1 What do you mean "pressed on clear"? – j08691 Commented Feb 2, 2012 at 19:38
  • When the button "clear" is click the file upload field should be cleared :) – Rails beginner Commented Feb 2, 2012 at 19:40
Add a comment  | 

4 Answers 4

Reset to default 8

How about:

$('input[value="Clear"]').click(function(){
    $('#blog_opload').val('');
});

Example: jsFiddle

You could also get rid of onclick="reset_html(\'imagec_' + imagec_index + '\')" if you use this.

Quick answer is replace it:

$("#clear").click(function(event){
  event.preventDefault();
  $("#control").replaceWith("<input type='file' id='control' />");
});

<input type="file" id="control" />
<a href="#" id="clear">Clear</a>

You can do simply

  $("#filuploadId")[0].value = "";

In your code you meant to use the ID selector but instead are using class selector.

$('.' + id).html( $('.' + id).html() );

should be

$('#' + id).html( $('#' + id).html() );

Updated jsFiddle

发布评论

评论列表(0)

  1. 暂无评论