我試圖通過ajax在我的webiste發送電子郵件。表單是模態(即時使用custombox)但由於某些原因,我發送的每個值都是空值。如何在模態內容中使用getElementById?JavaScript與阿賈克斯值
鏈接custombox Git的樞紐:https://github.com/dixso/custombox
function _(id) { return document.getElementById(id);}
function submitForm() {
_("mybtn").disabled = true;
var formdata = new FormData();
formdata.append("n", _("n").value);
formdata.append("e", _("e").value);
formdata.append("m", _("m").value);
var ajax = new XMLHttpRequest();
ajax.open("POST","php/parser.php");
ajax.onreadystatechange = function() {
if(ajax.readyState == 4 && ajax.status == 200) {
if(ajax.responseText == "success") {
alert("Message has been send.");
} else {
btn.disabled = false;
}
}
}
ajax.send (formdata);
}
HTML:
<!-- Modal -->
<section class="modal" id="myModal">
<!-- Modal content-->
<div class="modal-content" id="modal-content">
<img class="modal-home-logo" src="img/logo.png" alt="logo">
<p id="modal-contact-title">Are you intrested in cooperation?</p>
<p id="modal-contact-sub-title">Just hit me up via this simple email form !</p>
<form id="my_form" onsubmit="submitForm(); return false;">
<p><input id="n" placeholder="Your name - HERE" required></p>
<p><input id="e" placeholder="Your email - HERE" type="email" required> </p>
<textarea id="m" placeholder="Tell me about your project... what is it? What do you hope to accomplish ? How can i help ? Oh try to ESC if missclicked :)" rows="10" required></textarea>
<p><input id="mybtn" type="submit" value="Let's do this !"></p>
<p id="modal-contact-sub-title">Awsome thanks !</p>
</form>
</div>
</section>
<!-- End Modal -->
的document.getElementById返回正確的HTML組件,但.value的功能無法正常工作,因爲它返回NULL。當我關閉我的模式時,一切正常。
那麼,如果你寫的document.getElementById( 'N')。值= 「SomeText」 則會在開發者控制檯它的工作原理,但的document.getElementById( 'N')。值返回NULL像往常一樣:(
嗯,我。除去custombox插件並提出模態動畫自己,所以它現在正
我們需要查看您的HTML來診斷問題。 – gyre
對,對不起 – robertbie
「可能重複」http://stackoverflow.com/questions/38907748/php-contact-form-reference-error。類似於這個其他問題太http://stackoverflow.com/questions/42352041/how-to-append-select-list-in-a-php-contact-form – HenryDev