如何遍历JSON,循环遍历DOM和JSON,以便将json元素与DOM中的元素进行匹配?
我有html:
<div class="input alpha grid_4">
<label for="enterEmail">Email Address<label>
<input id="enterEmail" name="enterEmail" type="text" />
<span class="validMark"></span>
</div>
在json渲染之后我想要它:
<div class="input alpha grid_4">
<label for="enterEmail">Email Address <a class="toolTip" href="#" title=" By providing us with an email address for your user name, you are more likely to remember it."></a></label>
<input id="enterEmail" name="enterEmail" type="text" />
<span class="validMark"></span>
<div class="helperText">john@test.com</div>
</div>
和json:
[
{
“field”: “enterEmail”,
“contextHelpText”: “By providing us with an email address for your user name, you are more likely to remember it.”,
“helperText”: “john@test.com”
},
{
“field”: “billAddress1”,
“contextHelpText”: “Please enter the address associated with your billing information.”
}
]
我在概念化循环如何运行时遇到问题,如果json中不存在helpertext或contexHelpText,如何解释。 (将它们放在json中更好,但没有价值吗?)
答案 0 :(得分:1)
var numFields = jsonData.length;
var i, field, $field, $helper;
for (var i = 0; i < numFields; i++) {
field = jsonData[i];
$field = $(field.field);
if (field.contextHelpText) {
$field.attr('title', field.contextHelpText);
}
$helper = $field.parent().children('.helperText');
if ($helper && field.helperText) {
$helper.html(field.helperText);
}
}
凌乱,但不能满足你的要求。
答案 1 :(得分:1)
也许是这样的:??? (jsFiddle)
$(function() {
var jDat = [
{
"field": "enterEmail",
"label": "Enter Email",
"contextHelpText": "By providing us with an email address for your user name, you are more likely to remember it.",
"helperText": "john@test.com"
},
{
"field": "billAddress1",
"label": "Bill Address 1",
"contextHelpText": "Please enter the address associated with your billing information."
}
];
for (i=0;i<jDat.length;i++) {
var newDiv = $("<div />").addClass("input alpha grid_4"), // or could draw from json data on if statemtn for the classes
newDat = jDat[i];
if (newDat['field']) {
var newLbl = $("<label />").attr("for", newDat.field).text(newDat.label).appendTo(newDiv), // you'll need to add data for label of each field
newInp = $("<input />").attr({ id: newDat.field, name: newDat.field, type: "text" }).appendTo(newDiv);
newSpn = $("<span />").addClass("validMark").appendTo(newDiv)
if (newDat['contextHelpText']) newLbl.append($("<a />").attr({ href: 'javascript:void(0)', title: newDat.contextHelpText }).addClass("toolTip"));
if (newDat['helperText']) newDiv.append($("<div />").addClass("helperText").text(newDat.helperText));
$("#contentArea").append(newDiv);
};
};
});
答案 2 :(得分:0)
我最终得到的是关于SO的大量输入和其他问题。
function addInfoHTML(data) {
if (typeof data != 'undefined' && data.length > 0) {
var i;
for (i = 0; i < data.length; i++) {
var $fieldDom = $('#' + data[i].field);
//make sure field dom exists
if ($fieldDom.length > 0) {
var parentDiv = $fieldDom.parents('div.input');
if (typeof data[i].contextHelpText != 'undefined') {
//find the first label from the input.div
var fieldLabel = $(parentDiv).find('label'),
$fieldLabel = $(fieldLabel),
ancHtml = " <a class='toolTip' href='#' alt='" + data[i].contextHelpText + "' title='" + data[i].contextHelpText + "'>some val</a>";
//append fieldLabel text(Enter Email) + ancHtml (tooltip)
$fieldLabel.html($fieldLabel.text() + ancHtml);
}
if (typeof data[i].helperText != 'undefined') {
var helperDiv = "<div class='helperText'>" + data[i].helperText + "</div>";
//append the helperDiv html at the end
$(parentDiv).append(helperDiv);
}
}
}
}
}
//on document ready call the addInfoHtml by passing json data
$(function(){
addInfoHTML(data);
});