2010-05-30 72 views
2

eHello大家,下面是我的代码以关闭“确定”按钮来显示一个jQuery的对话窗口:如何把按钮在HTML页面中一个jQuery对话框

<script type="text/javascript"> 
$(function(){ 
    $("#dialog").dialog({ 
     autoOpen:false, 
     bgiframe:true, 
     buttons: { "OK": function() { $(this).dialog("close"); } }, 
     width:500, 
     height: 350, 
     modal: true, 
     show: 'slide', 
     hide:'slide', 
     title:"Similar Trends Detected in 2nd DataSet" 
    }); 

    $("#userid").focus(); 
}); 

function showForm(matches){ 
    $("#dialog").html(matches).dialog("open"); 
} 

目前,它运行由供应字符串变量“匹配”,那么变量的内容会显示在对话框中。 现在我和我的队友想扩展这个对话框,我们想要在html内容的每一行附加一个按钮(“matches”变量),请注意我们不想在对话框中使用按钮(就像另一个“确定“按钮),但是我们想要框架内的按钮(实际的html内容)。

所以我想在这里得到一些帮助,我该如何修改我的“匹配”变量,以便在对话框中显示按钮。 谢谢。

回答

2

编辑:更新基于的评论从OP

function showForm(matches){ 
     // Of course, you'll need to modify with your own button. 
     // I also added a valid <br>, assuming you want it there. 
    matches = matches.replace(/<\/br>/g, '<button>my button</button><br>'); 

    $("#dialog").html(matches).dialog("open"); // Insert new HTML content 
} 

是否matches变量包含HTML?

你可以只让一个jQuery对象出来,并遍历它像任何其他HTML:

function showForm(matches){ 
     // Of course, you'll need to modify with your own button. 
     // I also added a valid <br>, assuming you want it there. 
    matches = matches.replace(/<\/br>/g, '<button>my button</button><br>'); 

    $("#dialog").html(matches).dialog("open"); // Insert new HTML content 
} 

相关jQuery的文档:

+0

嘿,帕特里克,thx。 我的匹配变量只是一个字符串变量,不包含任何HTML,我仍然可以按照你的方法吗? 谢谢。 – Kevin 2010-05-30 19:15:45

+1

@Robert - 不,如果你没有任何HTML,那么jQuery的选择器对你来说不会有任何好处。你将不得不使用正则表达式。我可以提供帮助,但需要查看文本样本。既然你说了一些关于线条的东西,我认为字符串中有一些东西表示线条的结尾? – user113716 2010-05-30 19:21:59

+0

是的,帕特里克,我的队友通过了我的变量匹配,这是一个字符串,行的末尾用“
”表示。我们应该怎么执行下一步,查找多个thx。 – Kevin 2010-05-30 19:50:20

2

你是什么意思的每一行?你可以发布匹配变量的样本值吗?为什么不把按钮包含在匹配字符串值中?

无论如何,您还可以为对话窗口小部件的“打开”事件提供回调函数。

$("#dialog").dialog({ 
    autoOpen:false, 
    bgiframe:true, 
    buttons: { 
     "OK": function() { 
      $(this).dialog("close"); 
     } 
    }, 
    width:500, 
    height: 350, 
    modal: true, 
    show: 'slide', 
    hide:'slide', 
    title:"Similar Trends Detected in 2nd DataSet", 
    open: function() { 
     var targetElements = 'br'; 
     $(this).find(targetElements).after('<button>click me</button>'); 
    } 
}); 

的内容每BR标签后,一个按钮将被后附...显示在对话框每次开回调将被触发。

+0

嗨,米糕,我跟着你的代码,但现在对话不显示。请给我一些想法? – Kevin 2010-05-30 19:18:12

+0

你仍然需要使用你的showForm()方法来打开对话框。 我会更新代码以适应'br'问题 – ricecake5 2010-05-31 03:59:18

1

所以比赛的内容是一些静态的HTML集的。一旦它被添加到DOM,您可以使用相同的选择器和控件用于其他任何事情。因此,让我们假设匹配字段包含元素列表。

function showForm(matches){ 
    $("#dialog").html(matches).dialog("open"); 
    var b = $("<input type='button' value='clickme'/>"); 
    $("#dialog ul li").append(b); 
} 

当然,这只有真正的工作,如果你有一些什么匹配包含的概念。如果你知道,例如它是一组具有特定类别的div,将有助于选择器。

相关问题