2012-08-24 51 views
0

我正在做一個使用jQuery的map函數的DOM操作。使用這個我試圖將一個元素附加到一個表單。jquery我其他如果部分不工作...什麼是錯的?

它工作正常只有一次;這意味着我只在if部分獲得結果,我無法從else if部分獲得任何值。我的功能出了什麼問題,還是我的方法錯誤?

我的數據:

"fields":[ 
      { 
       "id":"firstname", 
       "label":"First Name", 
       "name":"fname", 
       "type":"text", 
       "value":"" 
      }, 
      { 
       "id":"email", 
       "label":"Email", 
       "name":"email", 
       "type":"text", 
       "value":"" 
      }, 
      { 
       "id":"countries", 
       "label":"Country", 
       "name":"countries", 
       "type":"select", 
       "options":[ 
        { 
        "value":"", 
        "text":"Select Country" 
        }, 
        { 
        "value":"in", 
        "text":"India", 
         "selected":"true" 
        }, 
        { 
        "value":"us", 
        "text":"United Stated" 

        }, 
        { 
        "value":"uk", 
        "text":"United Kingdom" 
        }, 
        { 
        "value":"cn", 
        "text":"Canada" 
        } 
       ] 
      }, 
      { 
       "id":"submit", 
       "name":"submit", 
       "type":"submit", 
       "value":"Submit" 
      } 
     ] 

我的功能:

var processModules = function (mData) { 
     $.map(mData, function (val,i) { 
      $(val.container).append(
       $(val.type === 'content' || val.type === 'navigation' ? 
       $('<div />',{ 
        'class':val.attributes['class'], 
        id:val.attributes.id}) 
       .append(val.title ? $('<h2 />',{text:val.title}) : "") 
       .append(val.subtitle ? $('<h3>',{text:val.subtitle}) : "") 
       : 
       val.type === 'form' ? $('<form />',{ 
        id:val.attributes.id, 
        'class':val.attributes['class'], 
        action:val.action, 
        name:val.name 
       }) 
       .append($('<legend />',{text:val.title})) 
       .append(
        $.map(val.fields, function (val,i) { 
var element; 
         if(val.id) { 
          console.log(val.id); // getting values and appending elements 
         } 
         if (val.label) { 
          element = $('<label />',{text:val.label}); // am not getting any value here.. 
         } 
         if (val.type) { 
          element = $('<input />',{id:val.id}) // I am only getting value here.. 
         } 
        }) 
       ) 
       : "")); 
     }) 
    } 

問題是什麼?誰能幫我?

+1

答案涵蓋了if/else的事情,但與你應該使用'$ .each()'而不是'$ .map()'無關。 '.each()'用於迭代數組/對象的每個元素/屬性,這就是你正在做的事情。 '$ .map()'也遍歷這些項目,但它的目的是根據您對舊項目進行的某些處理來創建一個新數組。 – nnnnnn

回答

1

else if只有在前面的if/else if語句不被調用時纔會被調用。例如:

if (true) { 
    // This code gets run 
} 

else if (true) { 
    // This code never will 
} 

考慮到這一點,改變你的if語句是這樣的:

if(val.id) { 
    console.log(val.id); 
} 

if (val.label) { 
    console.log(val.label); 
} 

if (val.type) { 
    console.log(val.type); 
} 
+0

我明白了你的觀點。我改變了我的代碼,但只有第一部分,如果條件追加,休息不工作... – 3gwebtrain

1

您的代碼正常工作,你就不會當第一,如果獲取到其他部分評估爲真。一旦if(val.id)返回true,所有其餘的將被跳過

+0

我改變了,如果條件,使用這個我想追加元素,但它不追加......我更新了我的代碼可以給你一個好方法嗎? – 3gwebtrain

+0

嘗試在if調用之前放置一個console.log,以查看爲'val.label'和'val.type'獲得的值,以瞭解它爲什麼不評估爲'true' – hsalama

相關問題