2016-02-27 81 views
0

我有一個javascript代碼,javascript代碼用於製作字體和顏色預覽。因此,您可以選擇字體(例如Arial)和顏色(例如紅色),代碼將使用您選擇的內容更改輸入框。這個選擇被自動保存併發送到購物車,在購物車中我有一個按鈕來編輯您的更改,當我按下該編輯按鈕時,我的問題出現在這裏,而我回到了我的選擇中,我看不到預覽。例如,當我按下編輯按鈕後,字體必須是Arial,並且顏色必須是紅色。我認爲jquery在我返回時不會執行。無論如何要解決這個問題嗎?加載窗口上的jQuery

enter image description here

這是jQuery代碼:

jQuery(document).ready(function() { 
    jQuery(".product-options dd .font select").change(function() { 
    var str = jQuery(this).find("option:selected").text(); 
    jQuery(".product-options dd input.input-text").css("font-family", str); 
    }); 
    jQuery(".product-options dd .color select").change(function() { 
    var str = jQuery(this).find("option:selected").text(); 
    jQuery(".product-options dd input.input-text").css("color", str); 
    }); 
}); 
+0

顯示你迄今爲止所做的。 – urfusion

+0

請把你所有的代碼放在這裏,比如html和css和js – Pedram

+0

嗨,我編輯我的帖子,我認爲這段代碼沒有初始化,當我回到頁面 – Robert

回答

1

所以,你想打電話頁面加載相同的代碼。在這種情況下,triggerHandler()是你的朋友。見下:

jQuery(document).ready(function() { 
    jQuery(".product-options dd .font select").change(function() { 
    var str = jQuery(this).find("option:selected").text(); 
    jQuery(".product-options dd input.input-text").css("font-family", str); 
    }); 
    jQuery(".product-options dd .color select").change(function() { 
    var str = jQuery(this).find("option:selected").text(); 
    jQuery(".product-options dd input.input-text").css("color", str); 
    }); 

    // add these two lines: 
    jQuery(".product-options dd .font select").triggerHandler('change'); 
    jQuery(".product-options dd .color select").triggerHandler('change'); 
}); 
+0

非常感謝你是一個像魅力一樣的工作 – Robert