jQuery UI对话框传递变量

问题描述:

我正在为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从产品,我希望它让我的对话框的打开功能。

我在正确的轨道上,有人可以帮我在那里得到那个变量。

在此先感谢。

设置一个变量如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

感谢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。