使用JSON的Javascript For循环HTML语法

时间:2013-08-27 18:45:33

标签: javascript html json

我对Javascript很陌生,所以我不确定我是否正确行事。首先,我使用JSON创建一个数组对象'providerlisting'。接下来我创建一个for循环,它应该遍历html,直到JSON数组中没有更多的列表。我不确定我是否正确地使用了语法。我也很想问这些问题,所以如果我做错了,我会提前感到抱歉。

for (var i=0;i<providerlisting.length;i++)
{ document.write('<div class="entry panel row">
                    <div class="large-4 columns first">
                        <div class="name">'providerlisting.nametitle[i]'</div>
                        <div class="specialty">'providerlisting.caretype[i]'</div>
                        <div class="peferred">'providerlisting.preferredprovider[i]'</div>
                    </div>
                    <div class="large-3 columns second">
                        <div class="address">'providerlisting.address1[i]'<br />
                        'providerlisting.address2[i]'<br />
                        'providerlisting.citystatezip[i]'
                        </div>
                    </div>
                    <div class="large-3 columns third">
                        <img src="'providerlisting.coverage[i]'" alt="example">
                    </div>
                    <div class="large-2 columns fourth">
                        <div class="status">'providerlisting.status[i]'</div>
                        <a data-dropdown="actionsMenu2" class="actions button small secondary round dropdown" href="#">Actions</a><br>
                        <ul id="actionsMenu2" data-dropdown-content="" class="f-dropdown">
                            <li><a href="'providerlisting.psn[i]'">Generate PSN</a></li>
                            <li><a href="'providerlisting.dcontact[i]'">Download Contact</a></li>
                            <li><a href="'providerlisting.save[i]'">Save to Provider List</a></li>
                            <li><a href="'providerlisting.rating[i]'">View Healthgrades Rating</a></li>
                        </ul>
                    </div>
                </div>
     ');

1 个答案:

答案 0 :(得分:1)

我无法确切地说,因为你还没有发布你的JSON,但是应该这样做:

for(var i = 0; i < providerlisting.length; i++) {
    document.write('<div class="entry panel row">
                    <div class="large-4 columns first">
                        <div class="name">'+ providerlisting[i].nametitle +'</div>
                        <div class="specialty">'+ providerlisting[i].caretype +'</div>
                        <div class="peferred">'+ providerlisting[i].preferredprovider +'</div>
                    </div>
                    <div class="large-3 columns second">
                        <div class="address">'+ providerlisting[i].address1 +'<br />
                        '+ providerlisting[i].address2 +'<br />
                        '+ providerlisting[i].citystatezip +'
                        </div>
                    </div>
                    <div class="large-3 columns third">
                        <img src="'+ providerlisting[i].coverage +'" alt="example">
                    </div>
                    <div class="large-2 columns fourth">
                        <div class="status">'+ providerlisting[i].status +'</div>
                        <a data-dropdown="actionsMenu2" class="actions button small secondary round dropdown" href="#">Actions</a><br>
                        <ul id="actionsMenu2" data-dropdown-content="" class="f-dropdown">
                            <li><a href="'+ providerlisting[i].psn +'">Generate PSN</a></li>
                            <li><a href="'+ providerlisting[i].dcontact +'">Download Contact</a></li>
                            <li><a href="'+ providerlisting[i].save +'">Save to Provider List</a></li>
                            <li><a href="'+ providerlisting[i].rating +'">View Healthgrades Rating</a></li>
                        </ul>
                    </div>
                </div>');
}

我将每个providerlisting.prop[i]更改为providerlisting[i].prop。 你也是以错误的方式串联在一起:

var str = 'some words'someVariable'more words'无效。

var str = 'some words'+ someVariable +'more words'会。