2016-11-15 64 views
1

我正在嘗試動態更改Jquery UI對話框的格式。我有一個數組,並根據「格式」參數我傳遞給createDialog函數,類應該改變(爲「好」,「紅色」的「壞」一流的「綠色」類):Jquery UI對話條件格式化

var array = [["bullet1","bad"],["bullet2","good"], ["bullet3","bad"],["bullet4","good"]]; 

$('#a-button').click(function() { 
var current = 0; 
//current dynamically changes in the longer version of the code 
var format = array[0][1]; 
createDialog(array[current][0], current, format); 
}); 

function createDialog(data, current, format) { 
var $div = $('<div>'); 
$div.html(data).dialog({ 

//condition I add which breaks the page 
if (array[current][1] == "bad") { format = "red"; 
} else { 
format = "green"; 
} 

dialogClass: format, 
width:"auto", 

}); 
} 

僅供參考我的CSS如下:

.ui-dialog.red { 
border: 3px solid red; 
} 

.ui-dialog.green { 
border: 3px solid green; 
} 

誰能給我正確的語法不破壞代碼,請(如果你刪除的條件的代碼將工作)添加這種狀況呢?

回答

0

您可以使用ternary operator使dialogClass聲明

function createDialog(data, current, format) { 
var $div = $('<div>'); 
$div.html(data).dialog({ 
    dialogClass: array[current][1] == "bad"?"red":"green", 
    width:"auto", 
}); 
}