每当使用我制作的此表单(葡萄牙语)时,刷新将从页面中删除所有先前输入的数据。为什么会发生这种情况,我该如何解决?谢谢你的时间。
<script type="text/javascript">
function submitForm(){
// Initiate Variables With Form Content
var nome = $("#nome").val();
var email = $("#email").val();
var telefone = $("#telefone").val();
var assunto = $("#assunto").val();
var mensagem = $("#mensagem").val();
$.ajax({
type: "POST",
url: "send-contact2.php",
data: "nome=" + nome + "&email=" + email + "&telefone=" + telefone + "&assunto=" + assunto + "&mensagem=" + mensagem,
cache:false,
success: function (data) {
alert(data);
}
});
}
</script>
<form id="myForm">
<div class="col col-md-6">
<input type="text" name="nome" id="nome" required value="" tabindex="1" placeholder="Nome">
<input type="text" name="email" id="email" required value="" tabindex="2" placeholder="E-mail">
<input type="text" name="telefone" id="telefone" required value="" tabindex="2" placeholder="Telefone">
<select id="assunto" name="assunto" required>
<option value="outros">Outros assuntos</option>
<option value="encomendas">Encomendas</option>
</select>
</div>
<div class="col col-md-6">
<textarea name="mensagem" id="mensagem" cols="29" rows="8" placeholder="Mensagem"></textarea>
</div>
<div class="col col-md-12 ">
<button name ="submit" type="submit" onclick="return submitForm();">Enviar</button>
</div>
</form>
答案 0 :(得分:5)
您的函数还需要return false
来阻止点击事件行为提交表单:
function submitForm(){
// your code...
return false;
}
更好的是,直接挂钩submit
的{{1}}事件并取消笨重的form
处理程序,并使用onclick
为您收集表单数据:
serialize()
<button name="submit" type="submit">Enviar</button>
答案 1 :(得分:0)
尝试将onClick
操作更改为onSubmit
并在方法后添加return false
。像这样:
<button name ="submit" type="submit" onsubmit="submitForm(); return false;">Enviar</button>
良好做法是将method="POST"
添加到表单属性中。