2013-03-14 34 views
0

我想設置一個對話框,我希望將按鈕的名稱按下並轉移到對話框中,但我不知道如何執行此操作。在jQuery中傳遞變量以更改對話框標題

到目前爲止,我有:

$(document).ready(function(){ 
$("#x").live('click', function(){ 
var name = $(this).attr('name'); //want to pass this to the next jquery 
$('#dialog_box').dialog("open"); 
}); 
var input = $("#input");  
$("#dialog_box").dialog({ 
    autoOpen: false, 
    resizable: false, 
    height:180, 
    width: 350, 
    modal: true, 
    buttons: { 
     "Update": function(){ 
      alert(name); 
     } 
    } 
    }); 
}); 

另外,我將如何去使用變量,一旦轉移到更新的對話框(如下標記)的稱號?

<div id="dialog_box" title="name variable here"> 
<form id="dialog_form" name="dialog_form"> 
    <fieldset> 
    <input type="text" name="input" id="input"/> 
    </fieldset> 
</form>     
</div> 

任何幫助表示讚賞:)

回答

1

所有.live()首先不贊成,我建議使用.on()並嘗試.data()方法

$(document).on('click', '#x', function(){ 
    $('#dialog_box').data('name', $(this).attr('name')); //passed to dialog 
    $('#dialog_box').dialog("open"); 
}); 

內部對話

$("#dialog_box").dialog({ 
     autoOpen: false, 
     resizable: false, 
     height:180, 
     width: 350, 
     title: $(this).data('name'), //will set dialog title 
     modal: true, 
     buttons: { 
     "Update": function(){ 
      alert($(this).data('name')); //should return input value 
     } 
    } 
    }); 
}); 
+1

謝謝,我肯定會考慮將來更多地使用data()方法。每天都是學校的一天:) – xgstr 2013-03-14 14:40:13

3

請更改文本框的ID與合適的名稱。

試試這個。

var titleValue=$("#input").val(); 
$("#dialog_box").dialog({ title: titleValue }); 

或 可以覆蓋默認這樣

$("#dialog_box").dialog("option", "title", $('#titleText').val()); 

按鈕單擊處理程序。

$('#btnOpendialog').on('click', function (e) { 
    e.preventDefault(); 
    $("#dialog_box").dialog('open'); 
    $("#dialog_box").dialog("option", "title", $('#titleText').val()); // to change the dialog title. 

}) 

Working Demo

+0

這集的標題值調用輸入。名稱變量將在按下按鈕時設置(每個名稱都有不同的名稱,從mysql表中生成)。感謝您的輸入 – xgstr 2013-03-14 12:59:05

+0

@xgstr我更新了一個工作演示。 – 2013-03-14 13:01:37

+0

感謝您的演示,但您沒有回答這個問題,@safarov提供了一個工作解決方案。謝謝你的時間。 :) – xgstr 2013-03-14 14:38:28