1

我低于 XML

<Root>
<field/>
<field/>
<field>
    <label>
        Have you invested before
    </label>
    <value>
        No
    </value>
    <label>
        Are you looking to Invest in the next 6 months
    </label>
    <value>
        Maybe
    </value>
</field>
<field>
    <label>
        What Investments are you interested in
    </label>
    <value>
        Carbon Credits, Green Investments
    </value>
</field>
<field>
    <label>
        How much are you looking to invest
    </label>
    <value>
        £250,000+
    </value>
</field>

从这里我需要HTML像下面这样渲染它。

Have you invested before: No
Are you looking to invest in the next 6 months: Maybe
What investments are you interested in: Carbon Credits, Green Investments
How much are you looking to invest: £250,000+

目前它的输出是:

:
:     
Have you invested beforeAre you looking to Invest in the next 6 months: YesMaybe
What Investments are you interested in:  Carbon Credits, Green Investments
How much are you looking to invest:    £250,000+

我在下面使用,jquery但它没有产生我期望的输出。

$(data).find('field').each(function (index, element) {
        debugger;
        //if (field.find('label').length > 0) {
        var field = $(element)
        if (field.find('label').length > 1) {

            var confirmationNumbers = $(field).find("label").map(function () {
                //debugger;
                list.append('<dt>' + label + ': </dt>').append('<dd>' + value + '</dd>');
                return $(this).text();
            });                
            //var label = field.find('label').text()
            //var value = field.find('value').text()
            //list.append('<dt>' + label + ': </dt>').append('<dd>' + value + '</dd>');
        }
        else {
            var label = field.find('label').text()
            var value = field.find('value').text()
            list.append('<dt>' + label + ': </dt>').append('<dd>' + value + '</dd>');
        }
        //}            
    });

我做错了什么..

4

1 回答 1

2

尝试

$(data).find('field label').each(function(){
    var $this = $(this);
    list.append('<dt>' + $this.text() + ': </dt>').append('<dd>' + $this.text().val() + '</dd>');
})

在 IE 上试试这个

$(data).find('field label').each(function() {
    var $this = $(this);

    list.append('<div><span>' + $this.text() + '</span><span>'
            + $this.text().val() + '</span></div>');
})
于 2013-04-22T11:37:28.210 回答