2014-11-06 76 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