2010-02-15 67 views
0

所以我提供幫助,這對其他問題,但我掙扎時,我的應用程序的複雜性進步來實現它...如何在jQuery中爲這些階段編寫乾的update_output()函數?

var gender; 
var age; 
$("#gender :radio").click(function() { 
    gender = $(this).val(); 
    update(); 
}); 
$("#age li").click(function() { 
    age = $(this).text(); 
    $('#children').show(); 
    update(); 
}); 


function update() { 
    var text = gender || ""; 
    if (text.length > 0) { 
    text += " -> "; 
    } 
    text += age || ""; 
    $("span.jquery_out").text(text); 
} 

這很好,但我想在12加變量,並且我需要以更合意的方式使update()函數時鐘運行。

我瓦爾是:

var gender; 
var age; 
var children; 
var income; 
var stage2select; 
var day; 
var spend; 
var categories; 
var product; 
var price; 
var quantity; 
var total; 

我將如何通過與update()函數獲取這些循環「 - >」每一個在兩者之間,沒有超過使用代碼?

謝謝,馬特。

+0

請,請解決您的shift鍵,我不會爲你下一次編輯。 – 2010-02-15 21:56:35

+0

嗨Koning,謝謝,但是什麼問題,只是爲了確保我不會再這樣做? – mdskinner 2010-02-16 02:05:03

+0

你又做了一次xD你在評論中用小寫字母寫了我。問題在於,閱讀時眼睛真的很痛苦。 – 2010-02-16 13:24:01

回答

1

你可以你的長期變量列表更改爲對象,然後循環對象是這樣的:

var myObj = { 
    gender:'', 
    age:'', 
    children:'', 
    income:'', 
    stage2select:'', 
    day:'', 
    spend:'', 
    categories:'', 
    product:'', 
    price:'', 
    quantity:'', 
    total:'' 
}; 

$("#gender :radio").click(function() { 
    myObj.gender = $(this).val(); 
    update(); 
}); 

$("#age li").click(function() { 
    myObj.age = $(this).text(); 
    $('#children').show(); 
    update(); 
}); 

function update() { 
    var text = ""; 
    $.each(myObj, function(i){ 
     if (this != ''){ 
      if (text.length){ 
       text += " -> "; 
      } 
      text += this; 
     } 
    }); 
    $("span.jquery_out").text(text); 
} 
+0

謝謝,只有「var out」應該是「var text」我猜對了嗎? – mdskinner 2010-02-16 02:00:59

+0

是的,我應該糾正它 – PetersenDidIt 2010-02-16 02:04:19

相關問題