Jump to content

Submit form with jquery


jfosorio

Recommended Posts

Boa tarde,

Estou com um problema algo estranho. Basicamente, tenho um formulário com uma div e um input do tipo file (com display: none). A ideia é que ao clicar na div, despoleta-se o trigger click do input para abrir a caixa do diálogo. Depois de se selecionar o ficheiro, pretende-se que o formulário seja automaticamente submetido. o código que desenvolvi funciona bem no chrome e no ff, mas por algum motivo, não funciona no ie 9.

A conclusão a que chego é que o submit() não funciona dentro do evento change(), só não entendo porquê...

		

<?php
if(isset($_POST['op']))
echo "Form submited!";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function(){
$("#img_placeholder").click(function(){
$("#file_input").click();
});

$("#file_input").change(function(){
if($("#file_input").val() != ""){
$("#insert_product_form").submit();
}
});
});
</script>
</head>

<body>
<form name="insert_product_form" id="insert_product_form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
<input type="file" name="file_input" id="file_input" style="display:none;" />
<div id="img_placeholder" style="width:100px; height:100px; border:3px dashed #CCC;">clique aqui para selecionar uma imagem</div>
<input type="submit" value="Save" />
<input type="hidden" name="op" />
</form>
</body>
</html>	

Será que alguém pode dar-me uma ajuda para resolver isto?

Cumprimentos!

Edited by jfosorio

CSS 3D Text - Partilha mensagens em 3D com os teus amigos no Facebook!

Portal de Informática it4life - Emprego, notícias, fórum, comunidade.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.