2014-11-06 74 views
-2

我有兩個訂單A和B. 當我點擊A時,它會打開一個對話框,其中有一個選擇選項,默認爲空白,但有Yes或No. 當我點擊B ,它也會打開一個帶有選擇選項的對話框;然而,它不是空白的默認值,它可以帶來我爲A選擇的任何東西。jQuery對話框以相同的選擇選項打開

我需要B在第一次點擊時打開一個新的對話框。我還需要A來記住用戶上次選擇的內容。我試過$("#input select option:selected").val("Y"),它確實設置了值;但不在UI本身。當我想根據是否選擇Y或N來隱藏元素時,這會導致問題。

+4

您可以張貼的jsfiddle什麼你目前有哪些? – 2014-11-06 21:52:52

回答

1

您可能希望將選定的選項保存在某處,並在對話框打開時將<select>的值設置爲該保存的值。

像下面的東西應該讓你開始。

var orderData = {}; // -- An object to save the data into 
var activeEl;  // -- Keep track of which button we've clicked 

$('#btnOrder1,#btnOrder2').click(function(){ 

    // -- open the dialog 
    $('#dialog').dialog({ 
     title:'Order Options', 
     modal: true 
    }); 

    // -- set the last clicked button to the ID of the one we clicked... 
    activeEl = this.id; 
    // -- set the select's option to the value we've stored. 
    // -- Unless we don't have a value, in which case we'll use "empty". 
    $('#selOrderOption').val(orderData[activeEl] || ''); 
}); 

// -- set up the save click handler 
$('#btnSave').click(function(){ 
    // -- save the selected value, keyed by the active button's ID 
    orderData[activeEl] = $('#selOrderOption').val(); 
    $('#dialog').dialog("close"); 
}); 

http://jsfiddle.net/daveSalomon/7Lds66td/

+1

非常感謝您這樣做。它實際上幫助我認識到,改變選擇器的值和所選選項的值之間存在差異。 – simplyzeke 2014-11-07 17:17:21