javascript - AJAX .val() 或 .text() 不适用于 textarea?
问题描述
我正在尝试使用 AJAX 请求将 textarea 数据发送到谷歌表单,但 .val() 似乎不适用于 textarea。我怎样才能解决这个问题?
我希望允许人们围绕一段信息输入内容,因此此时使用 textarea 至关重要。
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="http://fonts.googleapis.com/css?family=Dosis|Ubuntu+Mono|Quicksand|Josefin+Sans|Montserrat|Francois+One|Marvel" rel="stylesheet" type="text/css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body style="background:white; font-family:Montserrat;">
<h1>Feedback/Review</h1>
<p>The Insane Ink Board would be happy to hear from you about our services! Leave feedback or a review using the form below.</p>
<form id="form" target="_self" onsubmit="return postToGoogle();" action="" autocomplete="off">
<label id = "namelabel">Name:</label><label id = "emaillabel">Email: </label><br>
<input id="nameField" name="entry.1803640717" placeholder="First / Last Name" type="text" required>
<input id="emailField" name="entry.63463603" placeholder="Enter Your Email" type="email" required><br><br>
<label id = "relationlabel">Relation:</label><label id = "choicelabel">Why are you contacting us?</label><br>
<input id="mobField" name="entry.1793553898" placeholder="Relation to Insane Ink (EX: customer)" type="text" required>
<select id="cinema" name="entry.26162353" placeholder="Select Cinema" required>
<option value="">Choose an Option</option>
<option value="">Question about Prices</option>
<option value="">Had Issues With The Process</option>
<option value="">Feedback for great work</option>
<option value="">Other Option/Not Listed</option>
</select><br><br>
<label>Comment Here:</label>
<textarea id="explainfield" name="entry.1396765295" rows = "10" cols = "110" required></textarea>
<button id="send" type="submit" class="common_btn">Submit Form</button>
</form>
<h3 id="success-msg" style="text-align: center !important; margin-top:190px !important; display:none; color:#fff"> Your Request has been recieved!</h3>
<style>
body {font-family: Arial, Helvetica, sans-serif;}
form {border: 3px solid #f1f1f1; font-family: QuickSand;}
input[type=text], input[type=email], select {
width: 49%;
font-size:20px;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
box-sizing: border-box;
}
button {
background-color: maroon;
color: white;
padding: 14px 20px;
margin: 8px 0;
border: none;
cursor: pointer;
width: 100%;
}
button:hover {
opacity: 0.4;
}
.cancelbtn {
width: auto;
padding: 10px 18px;
background-color: #f44336;
}
.imgcontainer {
text-align: center;
margin: 24px 0 12px 0;
}
img.avatar {
width: 20%;
border-radius: 50%;
}
.container {
padding: 16px;
}
span.psw {
float: right;
padding-top: 16px;
}
#namelabel, #relationlabel {
margin-right: 30px;
}
#emaillabel {
margin-left: 385px;
}
#choicelabel {
margin-left: 340px;
}
/* Change styles for span and cancel button on extra small screens */
@media screen and (max-width: 300px) {
span.psw {
display: block;
float: none;
}
.cancelbtn {
width: 100%;
}
}
</style>
<script>
function postToGoogle() {
var field1 = $("#nameField").val();
var field2 = $("#emailField").val();
var field3 = $("#mobField").val();
var field4 = $("#cinema option:selected").text();
var field5 = $("#emailfield").text();
if(field1 == ""){
alert('Please Fill Your Name');
document.getElementById("nameField").focus();
return false;
}
if(field2 == ""){
alert('Please Fill Your Email');
document.getElementById("emailField").focus();
return false;
}
if(field3 == "" || field3.length < 1){
alert('Please Fill Your Mobile Number');
document.getElementById("mobField").focus();
return false;
}
if (field5 == "" || field5.length < 1 {
alert('Please comment down below');
document.getElementById("explainfield").focus();
return false;
}
$.ajax({
url: "https://docs.google.com/forms/d/e/1FAIpQLSdTNZewdXbyY51ueD9OEBi4Cca3JkVT1p4CykMSPyfzRpWHyQ/formResponse?",
data: {"entry.1803640717": field1, "entry.63463603": field2, "entry.1793553898": field3, "entry.26162353": field4, "entry.1396765295": field5},
type: "POST",
dataType: "xml",
success: function(d)
{
},
error: function(x, y, z)
{
$('#success-msg').show();
$('#form').hide();
}
});
return false;
}
</script>
</body>
</html>
抱歉,如果此代码太长,我不确定现在问题出在哪里。我对 AJAX 很陌生,所以这也可能是一个很大的因素。
解决方案
这就是你如何获取文本区域值。
var textareaVal = $('textarea#explainfield').val();
推荐阅读
- python - 如何遍历由 getter 或 @property 定义的对象的属性?
- java - 这两种从 LocalDateTime 创建 DateTime 的方法有什么区别?
- perl - 如何拆分多个文件名并在输出中获取姓氏
- laravel - Laravel:`php artisan app:name MyApp` 导致异常:内核不存在
- javascript - 当函数稍后出现错误时,console.log 不起作用
- visual-studio - 如何在 Visual Studio 中测量 Fortran 代码指标?
- excel - 如何更改选定单元格的值?
- spring-cloud - 将 Zuul、Hystrix(和 Feign)与 Spring Cloud HATEOAS 一起使用时如何转发标头?
- javafx - 如何设置组合框的文本颜色?
- corda - 如何解决 Corda 4 升级中的“找不到合同附件”异常