2012-06-01 45 views
0

我的應用程序與許多模態窗體對話框一起工作,我想通過調用函數創建模態窗體對話框,但是當我在對話框上單擊確定按鈕時出現錯誤「i.apply不是函數」 。我下面在函數中創建模態窗體對話框

的html代碼:

<div id="dlg_srch_acnt"> 
     <table cellpadding="0" cellspacing="0" border="0" class="display" id="dtl_acnt_srch" style=" padding-bottom:0px;"> 
      <thead> 
       <tr> 
        <th>Id</th> 
        <th>Account name</th> 
       </tr> 
      </thead> 
      <tbody> 
      </tbody> 
     </table> 
    </div> 

腳本

function init_dlg(id, autoOpen, height, width, modal, fn_button1) 
      { 
       id.dialog({ 
        autoOpen:autoOpen, 
        height:height, 
        width:width, 
        modal:modal, 
        buttons:{ 
         'OK':fn_button1, 
        }, 
        close:fn_close 
       }); 
      } 



function fn_ok() 
      { 
       $('#parnt_acnt').val(acnt_name); 
       $('#dlg_srch_acnt').('close'); 
      } 

init_dlg($('#dlg_srch_acnt'), false, '440', '480', true, 'fn_ok()'); 
+0

對不起,是 「遲到了」,但也許我的回答將與任何揮之不去的問題有所幫助。 – gibberish

回答

0

$('#dlg_srch_acnt')爲你傳遞一個jQuery對象給你的函數。所以你需要使用jQuery來包裝你的ID這樣

function init_dlg(id, autoOpen, height, width, modal, fn_button1) 
      { 
       $(id).dialog({ // Jquery wrapping should be done . 
        autoOpen:autoOpen, 
        height:height, 
        width:width, 
        modal:modal, 
        buttons:{ 
         'OK':fn_button1, 
        }, 
        close:fn_close 
       }); 
      } 
+3

爲什麼?在這種情況下,'id'是不是'jQuery'對象? –

+0

init_dlg($('#dlg_srch_acnt'),false,'440','480',true,'fn_ok()');我通過身份證是$('#dlg_srch_acnt') – Hainlp

3

我的說法相信你$('#dlg_srch_acnt').('close');應該$('#dlg_srch_acnt').dialog('close');

又通功能fn_ok名不fn_ok()因爲執行它的價值。

init_dlg($('#dlg_srch_acnt'), false, '440', '480', true, 'fn_ok'); 

什麼是你fn_ok功能acnt_name?要麼定義它(在使用它之前),要麼傳遞一個字符串,而不是一個變量名。

+0

我更正了$('#dlg_srch_acnt')。dialog('close');但沒有任何變化 – Hainlp

+0

@Hainlp現在檢查..我在你的代碼中發現了一些其他問題.. –

+0

@ Rajat Singhal:acnt_name是輸入字段。沒關係。你可以只用alert命令測試fn_ok:alert(「OK」);.我認爲這個問題從函數調用函數 – Hainlp

0

如何fn_button1後也許去除逗號,因爲你只有一個按鈕

buttons:{ 
      'OK':fn_button1 
     }, 
+1

昏迷在fn_button1是Dialog的語法之後,它會分離按鈕並關閉 – Hainlp

0

有幾件事錯在這裏。

Working jsFiddle Demo

0.1。 Rajat指出,$('#dlg_srch_acnt').('close');應該是$('#dlg_srch_acnt').dialog('close');

.2。 Rajat 差不多正確的關於傳遞函數名稱。應該是這樣的(不含引號): init_dlg($('#dlg_srch_acnt'), false, '440', '480', true, fn_ok);

.3。當引用從jQUI對話框對象內外部功能,你仍必須將它們包裝在功能塊:

buttons: { 
     'OK': function(){ 
      fn_button1(); 
     } 
    }, 
    close: function(){ 
     fn_close(); 
    } 

*請注意,你沒有fn_close在原崗位*

這裏定義爲完整的代碼,與一些附加位,以顯示它的工作(特別是觀看鮑勃在DLG接近更改爲簡):

HTML:

<input id="parnt_acnt" type="text" value="bob" /> 
<div id="dlg_srch_acnt"> 
    <p>Inside the dialog</p> 
</div> 

使用Javascript/jQuery的:

var acnt_name = "jane"; 

function init_dlg (id, autoOpen, height, width, modal, fn_button1) { 
    id.dialog({ 
     autoOpen: autoOpen, 
     height: height, 
     width: width, 
     modal: modal, 
     buttons: { 
      'OK': function(){ 
       fn_button1(); 
      } 
     }, 
     close: function(){ 
      fn_close(); 
     } 
    }); 
} 

init_dlg($('#dlg_srch_acnt'), false, '440', '480', true, fn_ok); 

function fn_ok() { 
    $('#parnt_acnt').val(acnt_name); 
    $('#dlg_srch_acnt').dialog('close'); 
} 

function fn_close() { 
    $('#dlg_srch_acnt').dialog('close'); 
    alert('Close fn accessed'); 
} 

Working jsFiddle Demo

相關問題