我有一个jquery,它为一个值返回未定义,但所有其他字段都可以。 请耐心等待,因为我已多次检查代码,但找不到问题。 “ sm”返回未定义,但姓名和电子邮件都可以。
<html>
<br>
<form>
Name:<br>
<input type="text" id="name">
<br>
Email:<br>
<input type="text" id="email">
<br><br>
Message:<br>
<input type="text" id=”sm”>
<br><br>
<button id="submitform">Add Record</button>
</form>
<p id="form-response">
<p id="form-response-2">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function() {
$("#submitform").click(function(e) {
e.preventDefault();
console.log('Button has been clicked');
var name = $("#name").val();
var email = $("#email").val();
var serverlessmessage = $("#sm").val();
console.log(serverlessmessage);
$.ajax({
type: "POST",
url: 'https://myurl.execute-api.ap-southeast-2.amazonaws.com/v1/',
contentType: 'application/json',
data: JSON.stringify({
'name': name,
'email': email,
'serverlessmessage': serverlessmessage
}),
success: function(res,status) {
$('#form-response').html('<div class="alert alert-info" role="alert">Record has been added...</div>');
$('#form-response-2').html(res.body);
console.log(res.body);
console.log(status);
},
error: function() {
$('#form-response').html('<div class="alert alert-info" role="alert">Something went wrong... We are working on it!</div>');
}
});
})
});
</script>
</body>
</html>
答案 0 :(得分:1)
您需要将<input type="text" id=”sm”>
替换为<input type="text" id="sm">
。添加ID时,您只能使用单引号或双引号。