[英]AJAX .val() or .text() does not work with textarea?
I'm trying to use AJAX requests to send textarea data into a google form, but .val() doesn't seem to work with textarea specifically.我正在尝试使用 AJAX 请求将 textarea 数据发送到谷歌表单,但 .val() 似乎不适用于 textarea。 How can I fix this?
我怎样才能解决这个问题?
I am looking to allow people to type up around a paragraph of information, so using textarea is vital at this point.我希望允许人们输入一段信息,因此此时使用 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>
Sorry if this code is too lengthy, I am not sure where the problems lies now.抱歉,如果这段代码太长,我不确定现在问题出在哪里。 I'm quite new to AJAX, so this may be a large factor as well.
我对 AJAX 很陌生,所以这也可能是一个很大的因素。
这就是您如何获取文本区域值。
var textareaVal = $('textarea#explainfield').val();
可以使用 val 方法获取 textarea 的值:
var message = $('textarea#message').val();
There was a Problem at有问题在
var field5 = $("#emailfield").text();
Field name is not proper字段名称不正确
var field5 = $("#explainfield").val();
$('#clickToGetValue').click(function(){ let idValue = $('#explainedFeildId').val(); let nameValue = $('textarea[name="explainedFeildName"]').val(); $('#selectedIdValue').text(idValue); $('#selectedNameValue').text(nameValue); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <textarea id='explainedFeildId' name='explainedFeildName'></textarea> <button id="clickToGetValue">Click to get value</button> <br/>Value by ID: <span id="selectedIdValue"></span> <br/>Value by NAME: <span id="selectedNameValue"></span>
If the value is assigned to a textarea then you can use .val()
There are two simple methods that return the value of textarea.如果将值分配给 textarea 则可以使用
.val()
有两种简单的方法可以返回 textarea 的值。
using by id where id is #explainfield
按 id 使用,其中 id 是
#explainfield
$("textarea#explainfield").val();
using by name where name is entry.1396765295
按名称使用,其中名称是
entry.1396765295
$('textarea[name="entry.1396765295"]').val();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.