2010-05-30 76 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,將有助於選擇器。

相關問題