[英]jQuery setting attribute value issue
I have some text boxes and on save , I am saving local storage data to them; 我有一些文本框,在保存时,我正在保存本地存储数据;
$(".formFieldUserData").each(function () {
var key = $(this).attr("name");
var value = $(this).attr("value");
localStorage.setItem(key, value);
}
Now for some reason, even though I enter some value in the text box, $(this).attr("value")
returns undefined
always. 现在由于某种原因,即使我在文本框中输入了一些值, $(this).attr("value")
总是返回undefined
。
What is the issue ? 有什么问题?
您可以使用.val()
而不是.attr("value")
检索表单字段值。
This is your solution: 这是你的解决方案:
$(".formFieldUserData").each(function() {
var key = $(this).attr("name");
var value = $(this).val();
localStorage.setItem(key, value);
});
Note: "value"
attribute of your tag is changable, so you use the $.val()
method to get it's value. 注意:标记的"value"
属性是可更改的,因此您使用$.val()
方法来获取它的值。
Try this 尝试这个
$(".formFieldUserData").each(function()
{
var key = $(this).attr("name");
var value = $(this).val();
localStorage.setItem(key, value);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.