1

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.” 
     }
]

ループがどのように実行されるかを概念化するのに問題があり、helpertextまたはcontexHelpTextがjsonに存在しない場合、それをどのように説明するか。(jsonにそれらを含める方が良いでしょうが、価値はありませんか?)

4

3 に答える 3

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);
  }
}

面倒ですが、要件をうまく処理することはできません。

于 2012-05-11T02:14:20.733 に答える
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);
        };
    };
});​
于 2012-05-11T02:47:39.603 に答える
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);
});
于 2012-05-11T14:37:10.007 に答える