English 中文(简体)
Internet Explorer file input onchange
原标题:

I have these dynamically created file input HTML elements which are used with a jQuery-ajax file upload plug-in.

I would like the file upload to start after the input value has been updated. However, Internet Explorer seems to ignore the Javascript onChange.

How can I achieve this in IE?

Example:

var html = $( <div class="add_input"> +
         <input type="file" name="file"/></div> ).change(submit);

$( #add_inputs ).prepend(html);
最佳回答

You could just replace the above with something along these lines

var html = $( <div class="add_input"><input type="file" name="file"/></div> );
$( #add_inputs ).prepend(html);
$("div.add_input > input[name= file ]").change(submit);

or

var html = $( <div class="add_input"><input id="filer" type="file" name="file"/></div> );
$( #add_inputs ).prepend(html);
$("#filer").change(submit);
问题回答

When you create elements from HTML in jQuery, the returned handle references the outermost element(s), so you re putting the change event handler on the <div>, not the <input>.

This works nonetheless in most browsers because in the DOM Level 2 Events specification, the change event ‘bubbles’ up the document to inform its parents when it changes, so an event handler on <div> will get informed of any changes on any of its child <input>s. However IE has its own event model in which change does not bubble.

An alternative:

($( <input type="file" name="file" /> )
    .change(submit)
    .prependTo( #add_inputs )
    .wrap( <div class="add_input"></div> )
);




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签