2012-11-29 30 views
2

一旦打開一個jQueryUI的對話框內變化jQueryUI的對話框按鈕文本,我想執行GET請求,並基於所述響應,改變按鈕的文本。經過數小時的掙扎後,我終於有了以下工作。這真的是最好的嗎?由於開放方法

$("#dialog").dialog({ 
    open  : function() { 
     var dialog=$(this); 
     $.get('ajax.php', function (data) { 
      var buttons=dialog.dialog("option", "buttons"); 
      buttons[1].text=(data==1)?"CANCEL":"CLOSE"; 
      var buttons=dialog.dialog("option", "buttons" ,buttons); 
     }); 
    }, 
    buttons  : [ 
     { 
      text : 'SAVE', 
      click : function() {} 
     }, 
     { 
      text : 'CANCEL', 
      click : function() {} 
     } 
    ]  
}); 
+0

爲什麼你不使你的ajax.php電話第一併在響應後打開對話框? – silly

+0

@silly。在ajax調用之後你會改變什麼?我如何複製整個按鈕對象,並用它重新初始化對話框? – user1032531

回答

2

您可以更改按鈕上的文字是這樣的...

下面給出的是沒有Ajax

function setbutton(button1, button2) { 
var btns = {}; 
btns[button1] = function() { 
    //your function 
    $(this).dialog("close"); 
}; 
btns[button2] = function() { 
    // Do nothing 
    //your function 
    $(this).dialog("close"); 
}; 

document.getElementById('dialogshow').innerHTML = "<div>open with given button text</div>"; 


$("#dialogshow").dialog({ 
    autoOpen: true, 
    width: 450, 
    height: 200, 
    modal: true, 
    position: 'center', 
    modal: true, 
    buttons: btns 
}); 
} 
$('.test').click(function() { 
setbutton('start', 'End');//in here button name you want.. 
}); 

See the Live Demo

+0

謝謝Dinesh。有趣的方法。它似乎比我的方法更清潔。 – user1032531

相關問題