我有一個頁面有一系列的輸入,類似於一個窗體,但沒有。按提交後,我想運行我的函數submitForm()。但是,每次頁面加載時,它都會自動點擊提交。關於如何阻止這種情況發生的任何想法?試圖停止從頁面加載自動提交按鈕
JS:
function submitForm(){
// Initiate Variables With Form Content
var fname = document.getElementById(name).value();
var femail = document.getElementById(email).value();
var fmessage = document.getElementById(message).value();
$.ajax({
type: "POST",
url: "cgi-bin/process.php",
data: "name=" + fname + "&email=" + femail + "&message=" + fmessage,
success : function(text){
if (text == "success"){
formSuccess();
}
}
});
}
function formSuccess(){
$("#msgSubmit").removeClass("hidden");
}
$(function() {
$("#btnContact").on("click", submitForm());
});
我的任何HTML:
<div class="row">
<label for="name" class="h4">Name</label>
<input type="text" class="form-control" id="name" placeholder="Enter name"
required="">
<label for="email" class="h4">Email</label>
<input type="email" class="form-control" id="email" placeholder="Enter email"
required="">
<label for="message" class="h4 ">Message</label>
<textarea id="message" class="form-control" rows="5" placeholder="Enter your message"
required=""></textarea>
</div>
<button id="btnContact" type="submit">Send Message</button>
<div id="msgSubmit" class="h3 text-center hidden">Message Submitted!</div>
</div>
嘗試更改按鈕類型,'type =「按鈕」' – ahoffner
我有沒有運氣 – Coleen