我已经完成了一个jquery UI对话框,它可以工作,但我需要将一个标签(名称)的值传递给jquery for the buttons,但是我一直在寻找和尝试许多不同的东西:(
我得到了什么:
标记(由PHP填充的值):
<a href="#" class="name">'.$name.'</a>
Html div:
<div id="name" title="view or send this user a message?"></div>
jquery的:
$(document).ready(function() {
$(function() {
$( "#name" ).dialog({
autoOpen: false,
width: 500,
show: {
effect: "fold",
duration: 500
},
hide: {
effect: "explode",
duration: 500
},
buttons: {
//Names need to go here as part of the buttons
"Send " + $(this).data('name') + " a message": function() {
$( this ).dialog( "close" );
},
"view " + $(this).data('name') + "profile ": function() {
$( this ).dialog( "close" );
}
}
});
$( "a.name" ).click(function() {
//Pass name to form
$("#name").data('name', $("a#name").text());
$( "#name" ).dialog( "open" );
});
});
});
所以我需要从#name获取名称,然后我尝试了.text()。到jquery用于按钮。
感谢您的帮助:)
答案 0 :(得分:3)
在对话框创建中存在一些问题,请尝试这种方式。
您在创建按钮时在对象属性中连接,这是无效的语法,并且在初始化期间不存在时尝试访问data('name')
。而是在需要时创建按钮,即在对话框显示之前单击超链接时,您知道哪些是需要附加到按钮的name
。
$(function () { // Same as document ready. only one of them is needed. No need to chain them
var dial = $("#name").dialog({
autoOpen: false,
width: 500,
show: {
effect: "fold",
duration: 500
},
hide: {
effect: "explode",
duration: 500
}
});
$("a.name").click(function () {
//Pass name to form
var name = $(this).text();
dial.dialog({
buttons: getButtons(name),
autoOpen:true
})
});
});
function getButtons(name) {
var dialog_buttons = {}; //Create the buttons here.
dialog_buttons["Send " + name + " a message"] = buttonCallBack; //set the call back.
dialog_buttons["view " + name + " profile"] = buttonCallBack; // assign different call back if required
return dialog_buttons;
}
function buttonCallBack() {
$(this).dialog("close");
}
答案 1 :(得分:1)
a
标记的选择器错误。改变
$("#name").data('name', $("a#name").text());
到
$("#name").data('name', $('a.name').text());