2012-12-22 82 views
0

我已經定義了function用於內部jqueryui dilaog顯示一些html內容的舊的輸入值:JQuery的保存對話框中被破壞後

function ui_window(title, dialogWidth, content,buttons,className) 
{ 
    $('<div id="window">').html(content). 
    dialog({ 
     title: title, 
     show: 'drop', 
     hide: 'drop', 
     width: dialogWidth, 
     buttons: buttons, 
     close: function (ev, ui) { $(this).remove(); }, 
     position: { my: 'center', at: 'center', of: window } 
} 
    ).addClass(className); 
    return false; 
} 

而這裏的另一個JavaScript function調用上述function

function checkForValidCommand() 
{ 
var content = getInnerHTML("#checkForValidCommand"); 
var myFunc = function() 
{ 
    var pensionerID = $('#txtID').val(); 
     alert(pensionerID); 
     $(this).dialog('destroy'); 
    } 
    else 
     $('#txtID').focus(); 
} 
var buttons = [{ text: 'OK', click: myFunc}]; 
ui_window("Pensioner", 410, content, buttons); 

} 

當我第一次打電話給「checkForValidCommand」並且input a value進入唯一的textbox然後按確定我得到一個值爲textbox的警報。然後當我再次撥打function,然後按下確定而沒有發送任何內容到textbox時,我仍然收到alert與舊的value。我的代碼中有什麼問題?

編輯:這裏是萬一html內容要看到:

<table> 
    <tr> 
    <td align="right"> 
     <label> 
      Insert the pensioiner's ID: 
     </label> 
    </td> 
    <td> 
    <input id="txtID" type="text" onkeydown="return onlyNumbers(event);" maxlength="8"/> 
    </td> 
    </tr> 
</table> 
+0

我發現它與對話框上的按鈕有關。因爲如果我關閉對話框而不按下按鈕,並在關閉事件時提醒txtID值,我會得到正確的值。 –

回答

1

OK.I感到非常高興回答這個問題我自己。所以問題是每當我打開一個帶有文本框的新jQuery對話框時,文本框的值就是第一個對話框中的值。不知何故,jquery保留了舊的價值。即使把$(this).dialog('destroy');也沒有解決問題(爲什麼這個命令不能完全刪除對話是另一回事)。這時我突然想起的$('#id')的工作原理:

It goes and selects the first matching element with the specified id.

這是必要的線索。我確信之前的對話並沒有從身體中移除。當我檢查身體時,我發現我是對的。所有的對話都在那裏,在身體裏。我嘗試了很多方法來完全刪除div,並停止在我發現的唯一一個工作。我所做的是把下面的代碼行顯示對話框之前:

$('body').find("#window").remove(); 

所以包裝函數來顯示一個jQuery對話框現在看起來是這樣的:

function ui_window(title, dialogWidth, content,buttons,className) 
{ 
    $('body').find("#window").remove(); 
$('<div id="window">').html(content). 
    dialog({ 
     title: title, 
     show: 'drop', 
     hide: 'drop', 
     width: dialogWidth, 
     buttons: buttons, 
     position: { my: 'center', at: 'center', of: window } 
} 
    ).addClass(className); 
    return false; 
} 

而且現在有隻在一個對話框一次。快樂jQuerying。

+0

其實我現在意識到我沒有爲destroy事件指定一個事件處理程序,而是爲close事件指定了一個事件處理程序,這就是爲什麼把'$('body')。find(「#window」)remove() ;'裏面的關閉事件處理程序沒有解決問題。單擊x按鈕並不會實際銷燬,而只是關閉一個對話框。 –