David Tran
David Tran

Reputation: 10606

Solve IE7 bug "input type=file" onchange fire twice?

Did you ever face this IE7's bug:

<input type="file" id="xxx">

<script> 
     $('#xxx').change(function(){ alert(1) })
</script>

when I click the input & pick a file, the alertbox shows the first time. Then I click on the blank area on the body, the alertbox shows once again. This happens even when I bind the change event to input:file with JQuery 1.6 (lastest at this moment).

How could I prevent this by the simplest way? Thanks for all suggestions!

Upvotes: 5

Views: 3504

Answers (2)

David Tran
David Tran

Reputation: 10606

This is what I've made after some hard hours of "brain storming" :

$('input:file').click(
    function(){
         $(this).one(
              'change',
              function(){ alert(1) /*do stuff here*/ }
         )
    }
)

Each time user click the input to choose file, we bind the event "change" one time with it. So the event fires exactly once everytime user want to pick a file.

And thanks god it works ! Thank you all.

Upvotes: 6

Narvath
Narvath

Reputation: 1

I would simply unbind the trigger once your function has ran

Upvotes: 0

Related Questions