2010-04-15 56 views
0

我正在爲Mysql中的表創建一個Web接口,並且想要使用jQuery對話框來輸入和編輯。我有以下代碼從開始:jQuery UI對話框傳遞變量

$("#content_new").dialog({ 
    autoOpen: false, 
    height: 350, 
    width: 300, 
    modal: true, 
    buttons: { 
     'Create an account': function() { 
      alert('add this product'); 
     }, 
     Cancel: function() { 
      $(this).dialog('close'); 
      $.validationEngine.closePrompt(".formError",true); 
     } 
    }, 
    closeText: "Sluiten", 
    title: "Voeg een nieuw product toe", 
    open: function(ev, ui) { /* get the id and fill in the boxes */ }, 
    close: function(ev, ui) { $.validationEngine.closePrompt(".formError",true); } 
}); 
$("#newproduct").click(function(){ 
    $("#content_new").dialog('open'); 
}); 
$(".editproduct").click(function(){ 
    var test = this.id; 
    alert("id = " + test); 
}); 

所以,當點擊與類「editproduct」的鏈接,它得到的ID從產品,我希望它讓我的對話框的打開功能。

我在正確的軌道上,有人可以幫我在那裏得到那個變量。

在此先感謝。

回答

2

設置一個變量如the_id腳本中的一切之上,試試這個代碼:

$("#newproduct").click(function(){ 
    $("#" + the_id).dialog('open'); 
}); 
$(".editproduct").click(function(){ 
    the_id = this.id; 
}); 
+0

我不認爲這是我想要的掃管笏。也許我應該更清楚。在我上面的代碼中沒有提到的對話框div是隱藏的,並且使用$(「#content_new」)對話框函數初始化。之後,它會在點擊帶有#newproduct的鏈接時打開。我也想要點擊編輯按鈕時打開相同的對話框。但是,然後我需要用戶點擊編輯的產品的ID,以正確的數據填充我的表單。所以,當點擊類.editproduct的鏈接時,我會得到點擊鏈接的id,並希望在我的對話框的打開功能中使用它。 – Dante 2010-04-15 09:32:03

+0

糟糕,你是正確的在腳本上設置變量。抱歉。該代碼是這樣的: \t $( 「#新品推薦」)點擊;(函數(){ \t \t $( 「#content_new」)對話框( '開放'); \t。}) 。 \t $( 「editproduct 」)點擊(函數(){ \t \t ID = this.id; \t \t $(「 #content_new」)對話框( '開放'); \t}); 謝謝你的幫助。 – Dante 2010-04-15 09:59:42

+0

@丹特:那是個好消息:) – Sarfraz 2010-04-15 10:01:18

0

感謝Sarfraz你是對的變量。對於其他人感興趣的全部代碼現在是:

$(document).ready(function() { 
var id = 0; 
$("#content_new").dialog({ 
    autoOpen: false, 
    height: 350, 
    width: 300, 
    modal: true, 
    buttons: { 
     'Create an account': function() { 
      alert('add this product'); 
     }, 
     Cancel: function() { 
      $(this).dialog('close'); 
      $.validationEngine.closePrompt(".formError",true); 
     } 
    }, 
    closeText: "Sluiten", 
    title: "Voeg een nieuw product toe", 
    open: function(ev, ui) { alert(id); }, 
    close: function(ev, ui) { $.validationEngine.closePrompt(".formError",true); } 
}); 
$("#newproduct").click(function(){ 
    $("#content_new").dialog('open'); 
}); 
$(".editproduct").click(function(){ 
    id = this.id; 
    $("#content_new").dialog('open'); 
}); 
$("#new").validationEngine();}); 

而在模式對話框的打開我得到正確的ID。