jQuery form submit event
I have a photo upload form that I'm working on that works like this:
- You click an "Upload Photo" button. jQuery detects the click and triggers a click event to a hidden "file" field within the form, which brings up a dialog to search through your files.
When the user selects a file, the change event is detected on the hidden file field and triggers a form submission. Here is the code:
// on photo upload button click: $("input[name=PhotoUpload]").click(function() { $("input[name=Photo]").click(); }); // on photo upload: $("input[name=Photo]").change(function() { $("form[name=MediaUpload]")开发者_如何学运维.submit(); });
and the form:
<form name="MediaUpload" method="post" action="/postad/media.cfm" target="Media" enctype="multipart/form-data"> <input type="button" name="PhotoUpload" value="Upload Photo"> <input type="file" name="Photo" size="30" style="visibility:hidden;"> </form>
This works great in Chrome and FireFox, but am having no luck in any version of IE. Any suggestions?
I would suggest trying this a little differently:
// on photo upload button click:
$("input[name=PhotoUpload]").click(function() {
$("#your_dialog").dialog("open");
});
//Remove the `.change` function.
// add the form submit line below after the dialog code changes the `Photo` field
$("form[name=MediaUpload]").submit();
It's easy to confuse IE with javascript. If it hits something that gives it indigestion, it stops processing the js.
精彩评论