[英]Adding localStorage data to a textbox
I have a form with information with user info:我有一个包含用户信息的表单:
<div>
<label for="nombre">Name</label>
<input type="text" required class="form-control" id="nombre" placeholder="Name" pattern="([A-Z][a-zA-Z]*)" />
</div>
<div>
<label for="apellido">Surname</label>
<input type="text" required class="form-control" id="apellido" placeholder="Surname" pattern="([A-Z][a-zA-Z]*)" />
</div>
<div>
<label for="email">Email</label>
<input type="email" required class="form-control" id="email" placeholder="Your email" />
</div>
<div>
<label for="domicilio">Mailing Address</label>
<input type="text" required class="form-control" id="domicilio" placeholder="Your address" />
</div>
<fieldset disabled>
<div>
<label for="comentario">Your information:</label>
<textarea id="comentario" rows="5" class="form-control"></textarea>
</div>
</fieldset>
<div>
<button type="submit" onclick="submitContacto()" class="btn btn-primary">Send</button>
</div>
This is the corresponding .js function这是对应的 .js 函数
function submitContacto() {
var nombre = $("nombre").value;
var apellido = $("apellido").value;
var email = $("email").value;
var domicilio = $("domicilio").value;
localStorage.nombre = nombre;
localStorage.apellido = apellido;
localStorage.email = email;
localStorage.domicilio = domicilio;
}
I wanted to know how I can make it so when you click the button, the information of the localStorage appears in the textarea.我想知道我是怎么做到的,所以当你点击按钮时,localStorage 的信息出现在 textarea 中。 I would like the textbox to show something like:
我希望文本框显示如下内容:
There's a few things going on here.. First - if you're using jQuery to select items by id, you must use the hashmark #这里有一些事情要做..首先 - 如果您使用 jQuery 按 id 选择项目,则必须使用哈希标记 #
$( "#nombre" )
Secondly, in jQuery, you would use the .val() function to get an object's value其次,在 jQuery 中,您将使用 .val() 函数来获取对象的值
var nombre = $( "#nombre" ).val
Lastly, you would set the value of the text-area using these same principles plus standard string manipulation.最后,您将使用这些相同的原则加上标准的字符串操作来设置文本区域的值。
$( "#comentario" ).val("Your Name: " + nombre + "\nYour Surname: " + apellido);
Or something to that effect, in your function.或者在你的函数中达到这种效果。 JSFiddle here
JSFiddle在这里
Use the below code to show the localstorage item in the text box:使用以下代码在文本框中显示 localstorage 项:
Put the code in button onclick将代码放入按钮 onclick
$('#nombre').val(localStorage.getItem('nombre') $('#nombre').val(localStorage.getItem('nombre')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.