jQuery每个Json值问题 [英] Jquery Each Json Values Issue
本文介绍了jQuery每个Json值问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<html>
<head>
<title>testjson</title>
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript">
var incidentReport1 = {
"text1": "n/a",
"text2": "n/a",
"text3": "n/a",
}
function readHtmlForInputs() {
var count = 0; //Setting count to 0
$('input').each(function(){
var input = $(this);
var temp = (input.attr('id'));
if(input.attr('type') == 'button'){alert('button');}
else{
incidentReport1.temp = input.val();
count++; //Incrementing counter
}
});
console.log("Input Types Found:" + count);
}
function saveChanges()
{
readHtmlForInputs();
console.dir(incidentReport1);
}
</script>
</head>
<body>
<input type="button" value="save" onclick="saveChanges();"/>
<input type="text" name="Surname" id="text1" class="InputText" /><br>
<input type="text" name="Surname" id="text2" class="InputText"/><br>
<input type="text" name="Surname" id="text3" class="InputText"/><br>
</body>
</html>
得到上面的代码块,我希望能够动态获取输入ID,并使用该ID在incdientReport1 json中分配一个值.当我这样做时,这个问题似乎就是这条线...
Got the above block of code, an i want to be able to dynamic take the inputs ID, and use the ID to assign a value in incdientReport1 json. When i do this issue seems to be this line...
var temp = (input.attr('id'));
分配ID并将其显示在控制台中时,效果很好
When assigning the ID and displaying it in console it works fine
但是当这行代码
incidentReport1.temp = input.val();
运行它将其保存到名为temp的新字段,而不是temp的字符串值.
Runs it saves it to a new feild called temp rather than the string value of temp..
那么困惑的人我要去哪里错了?
So confused guys where am i going wrong?
推荐答案
您要:
incidentReport1[temp] = input.val();
当您需要使用计算所得的属性名称时,请使用[ ]
运算符.也就是说,
When you need to use a computed property name, you use the [ ]
operator. That is,
object.propertyName
与
object[ "propertyName" ]
这篇关于jQuery每个Json值问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文