2013-03-14 64 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