jQuery中的ASP样式转发器

时间:2012-05-08 21:07:43

标签: javascript jquery cordova

我正在尝试'循环'结果,例如以下html是使用javascript设置的。 (在Javascript中解析XML)但我无法弄清楚如何一次循环遍历多个变量。基本上我一直把姓氏循环和循环放在一行。

 <div class="files-box">
         <img class="replace-2x file-image" width="32" src="images/files/doc.png" alt="img">
             <p class="file-title" id="list">[SURNAME]</p>
             <a href="#" class="file-href file-open">[PHONE NUMBER]</a>
             <a href="#" class="file-href">[CALLLINK]</a>       
             <div class="clear"></div>
             </div>   
     </div>

这是我的Javascript

function onDeviceReady()
    {

        $.ajax({
               type: 'GET',
               url: 'Lookupbysurname.aspx?surname=beech',
               dataType: 'xml',
               success: function(xmlDoc) {
               var $xml = $(xmlDoc);
               $xml.find('Surname').each(function() {
                                           $("#list").append($(this).text());
                                           });
               }
               });

    }

    </script>

基本上我试图循环遍历xml结果(姓氏,电话号码和callink)并在每次重新创建html,并将[SURNAME]等变量替换为正确的结果。它将进入一个手机应用程序

编辑:根据要求,下面是XML布局

<Results>
    <Result>
        <FirstName>Tom</FirstName>
        <Surname>Bedh</Surname>
        <Company>Company INC/Company>
        <Job_Title>Test Title</Job_Title>
        <callID>10582</callID>
        <CompanyID>10001</CompanyID>
    </Result>
</Results>

继承当前布局中发生的事情

enter image description here

1 个答案:

答案 0 :(得分:0)

尝试这样的事情。首先,您需要更改HTML结构:

<div id="container">
<div class="files-box">
     <img class="replace-2x file-image" width="32" src="images/files/doc.png" alt="img">
     <p class="file-title surname">[SURNAME]</p>
     <a href="#" class="file-href file-open phone-number">[PHONE NUMBER]</a>
     <a href="#" class="file-href call-link">[CALLLINK]</a>       
     <div class="clear"></div>
</div>   
</div>
​

jQuery的:

var $block = $('#container .files-box').remove();

$xml.find('Result').each(function() {
    var $result = $(this),
        $myblock = $block.clone();

    $myblock.find(".surname").text($result.find('Surname').text());
    $myblock.find(".phone-number").text($result.find('Phone').text());
    $myblock.find(".call-link").text($result.find('callID').text());
    $('#container').append($myblock);
});​

http://jsfiddle.net/mblase75/aKJYZ/