2013-05-09 75 views
0

看看這個jfiddle: 選擇一個選項,然後選擇另一個選項,您會看到重置爲上一個的警報。 如果您選擇「選擇一個選項」它做我想要的,但我不知道如何爲每個選定的選項做到這一點。Jquery select option value reset to previous

任何指導將非常感激!

+0

編輯此問題的人刪除了所有的代碼和鏈接? – rahularyansharma 2013-05-09 05:40:01

回答

0

我想可能是因爲這個改變,因爲簡單:

var price_name = jQuery(this).attr('name'); 

var price_name = jQuery(this).val(); 

(可能是不必要的,但這裏是一個更新的小提琴:http://jsfiddle.net/k36Qt/1/

+0

差不多,不完全。現在,它會不斷增加值。 – user2364330 2013-05-09 04:24:16

+0

當您選擇「選擇一個選項..」選項時,它會刪除所選的最後一個選項值。 – user2364330 2013-05-09 04:26:00

+0

我確定它只是一個這樣的小代碼,但我正在對着桌子砸我的頭,試圖弄清楚我能做什麼,而不用重寫這些東西。 – user2364330 2013-05-09 04:26:34

0

嘗試

var prevValue = 0; 
jQuery("select").change(function() { 
    var text_selectbox = jQuery(this).find(":selected").text(); 
    var price_process = 0; 
    var r = /[0-9.]+/g; 
    var s = text_selectbox; 
    var m; 
    while ((m = r.exec(s)) != null) { 
     price_process = m[0] ; 
    } 
    var price = parseFloat(price_process); 
    price = $.isNumeric(price) ? price : 0; 

    var product_price = parseFloat(jQuery("p.price span.amount").html().replace('$','')); 
    var total_price = product_price; 

    total_price = total_price - prevValue + price; 
    prevValue = price; 

    jQuery("p.price span.amount").html("$" + total_price); 

}); 

演示版:Fiddle

+0

謝謝您!它可以工作,但如果我選擇「選擇一個選項..」,我會得到一個$ NaN,我可以做些什麼來恢復原來的狀態? – user2364330 2013-05-09 04:36:07

+0

@ user2364330請參閱更新 – 2013-05-09 04:47:53