由于某种原因,此代码有效:
$(document).ready(function () {
var name = document.getElementById('Name').value;
var email = document.getElementById('Email').value;
var type = document.getElementById('Type').value;
var message = document.getElementById('Message').value;
$.post("send.php", {
Name: "Matt",
Email: "xxxxxx@hotmail.com",
Type: "1",
Message: "GO"
},
function (e) {
alert(e);
});
});
但是这段代码没有:
$(document).ready(function () {
$('#btn').click(function () {
var name = document.getElementById('Name').value;
var email = document.getElementById('Email').value;
var type = document.getElementById('Type').value;
var message = document.getElementById('Message').value;
$.post("send.php", {
Name: "Matt",
Email: "xxxxxx.wsu@hotmail.com",
Type: "1",
Message: "GO"
},
function (e) {
alert(e);
});
});
});
另外,我检查了我#btn
的,并且 id 是正确的,所以不是这样。第一个代码返回成功并发送电子邮件。第二个什么都不做,我不能很好地调试它,因为来自服务器的响应没有显示在我的 Chrome 开发者工具网络查看器中。
这是html表单:
<form id="form" action="" method="POST">
<label for="Name">Name: </label>
<input type="text" name="Name" id="Name">
<label for="Email">Email: </label>
<input type="text" name="Email" id="Email">
<label for="Type">Type: </label>
<select name="Type" id="Type">
<option value="0">General</option>
<option value="1">I need a consultation</option>
<option value="2">I need a website</option>
</select>
<label for="Message">Message: </label>
<textarea rows="10" cols="50" name="Message" id="Message"></textarea>
<br />
<button id="btn" class="submitBtn">Send</button>
</form>