[英]JQuery key is undefined in local storage
Wanting to be able to have multiple people take a survey then refresh etc. Need multiple people to be able to store a survey based on their name so each survey result is not overwritten when re-submitted. 希望能够有多个人进行调查,然后刷新等。需要多个人能够根据他们的姓名存储调查,因此重新提交时每个调查结果都不会被覆盖。 Trying to get the keys stored for each text box output 'person'.'name' = value. 试图获取为每个文本框存储的键输出'person'。'name'= value。 instead im getting a undefined.name = value 相反我得到一个undefined.name =值
<p>
<label>Name:</label> <input name="MainName" type="text"/>
</p>
$('form').submit(function() {
var person = $("#MainName").val();
$('input, select, textarea').each(function() {
var value = $(this).val(),
name = $(this).attr('name');
localStorage[person + "." + name] = value;
console.log('stored key: '+name+' stored value: '+value);
});
});
If the problem doesnt show above here is the whole: http://jsfiddle.net/5sG8v/ 如果问题没有在上面显示,那么这里是整体: http : //jsfiddle.net/5sG8v/
You're getting undefined
returned because you're trying to reference an ID that does not exist: MainName
is the name
of your input field, not the id.
由于尝试引用不存在的ID,所以返回undefined
返回值: MainName
是输入字段的name
,而不是id.
Give it an ID of MainName
, or whatever tickles your fancy: 给它一个MainName
的ID,或给您带来MainName
任何东西:
<label>Name:</label> <input id="MainName" name="MainName" type="text"/>
The #
is for selecting elements with a given id
attribute. #
用于选择具有给定id
属性的元素。 Your input
doesn't have an id
. 您的input
没有id
。
You could give the element an id
, or select based on the name
attribute: 您可以为元素指定一个id
,或者根据name
属性进行选择:
$('input[name="MainName"]');
You have used $("#MainName").val();
您已使用$("#MainName").val();
to get the value of element having ID MainName
. 获取ID为MainName
的元素的值。
#
is used to access element with IDs but you haven't defined that ID in html. #
用于访问具有ID的元素,但您尚未在html中定义该ID。
So define the ID attribute of the textbox like this : 因此,像这样定义文本框的ID属性:
<label>Name:</label> <input id="MainName" name="MainName" type="text"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.