Jquery 在div/modal框中加载变量的内容

Jquery 在div/modal框中加载变量的内容,jquery,Jquery,原始完整代码可在 提前感谢您提供的任何建议和帮助更新:很抱歉,您错过了您最关心的对话部分。请参见末尾的特定于对话框的添加 原始答案: 要将HTML从变量加载到元素中,请使用以下函数: $dialog.load($link.attr('href') + ' #content').dialog({ title: $link.attr('title'), width: 500, height: 300

原始完整代码可在


提前感谢您提供的任何建议和帮助

更新:很抱歉,您错过了您最关心的对话部分。请参见末尾的特定于对话框的添加


原始答案

要将HTML从变量加载到元素中,请使用以下函数:

$dialog.load($link.attr('href') + ' #content').dialog({
              title: $link.attr('title'),
                width: 500,
                height: 300
            });
如果您真的只是获取那里的所有内容,那么您使用的
load
函数就是这样做的:

$.ajax({
    url: "your.url",
    success: function(data) {
        $("#targetElement").html(data);
    },
    error: function() {
        // ... deal with error ...
    }
});
…但是如果您想先用它做些别的事情,请使用
ajax

例如,假设您正在使用JSON符号从服务器加载一些数据。假设数据如下所示:

$("#targetElement").load("your.url");
您可以这样做:

{
  "foo": "I'm the foo value",
  "bar": "I'm the bar value"
}​​​​​​​​
因此,使用上面的
ajax
调用:

function showDialog(content) {
  // Get the dialog element
  var dlgElement = $("#modal-dialog");

  // Update the dialog with the content
  dlgElement.find('p:first').html(content);

  // Show it
  dlgElement.dialog({
    height: 140,
    modal: true
  });
}
//加载我们的内容
$.ajax({
url:“http://jsbin.com/urebu5",
数据类型:“json”,
成功:功能(数据){
//显示数据的“foo”部分
showDialog(data.foo);
},
错误:函数(xhr、statusText、ex){
//显示错误
showDialog(“运行ajax调用时出错”);
}
});

如果要从头开始创建对话框,只需创建元素,然后确保完成后将其删除:

// Load our content
$.ajax({
  url: "http://jsbin.com/urebu5",
  dataType: "json",
  success: function(data) {

    // Show the 'foo' part of the data
    showDialog(data.foo);

  },
  error: function(xhr, statusText, ex) {

    // Show the error
    showDialog("Error running <tt>ajax</tt> call");
  }
});
函数createDialog(标题、内容){
//创建我们的对话结构
返回$(“”)
.css(“显示”、“无”)
.attr(“标题”,标题)
.html(“”+content+“

”) .附录(文件正文); } 函数显示对话框(dlg,销毁){ 变量选项={ 身高:140, 莫代尔:对 }; 如果(销毁){ opts.close=函数(事件,用户界面){ $(this.remove(); }; } 对话框(opts); }
使用:

//加载我们的内容
$.ajax({
url:“http://jsbin.com/urebu5",
数据类型:“json”,
成功:功能(数据){
//使用数据的“foo”部分创建对话框
var dlg=createDialog(“Foo数据”,Data.Foo);
//表现出来
showDialog(dlg,true);
},
错误:函数(xhr、statusText、ex){
//使用数据的“foo”部分创建对话框
var dlg=createDialog(
“Foo数据-错误”,
“运行ajax调用时出错”
);
//表现出来
showDialog(dlg,true);
}
});

更新:抱歉,错过了您最关心的部分是对话框。请参见末尾的特定于对话框的添加


原始答案

要将HTML从变量加载到元素中,请使用以下函数:

$dialog.load($link.attr('href') + ' #content').dialog({
              title: $link.attr('title'),
                width: 500,
                height: 300
            });
如果您真的只是获取那里的所有内容,那么您使用的
load
函数就是这样做的:

$.ajax({
    url: "your.url",
    success: function(data) {
        $("#targetElement").html(data);
    },
    error: function() {
        // ... deal with error ...
    }
});
…但是如果您想先用它做些别的事情,请使用
ajax

例如,假设您正在使用JSON符号从服务器加载一些数据。假设数据如下所示:

$("#targetElement").load("your.url");
您可以这样做:

{
  "foo": "I'm the foo value",
  "bar": "I'm the bar value"
}​​​​​​​​
因此,使用上面的
ajax
调用:

function showDialog(content) {
  // Get the dialog element
  var dlgElement = $("#modal-dialog");

  // Update the dialog with the content
  dlgElement.find('p:first').html(content);

  // Show it
  dlgElement.dialog({
    height: 140,
    modal: true
  });
}
//加载我们的内容
$.ajax({
url:“http://jsbin.com/urebu5",
数据类型:“json”,
成功:功能(数据){
//显示数据的“foo”部分
showDialog(data.foo);
},
错误:函数(xhr、statusText、ex){
//显示错误
showDialog(“运行ajax调用时出错”);
}
});

如果要从头开始创建对话框,只需创建元素,然后确保完成后将其删除:

// Load our content
$.ajax({
  url: "http://jsbin.com/urebu5",
  dataType: "json",
  success: function(data) {

    // Show the 'foo' part of the data
    showDialog(data.foo);

  },
  error: function(xhr, statusText, ex) {

    // Show the error
    showDialog("Error running <tt>ajax</tt> call");
  }
});
函数createDialog(标题、内容){
//创建我们的对话结构
返回$(“”)
.css(“显示”、“无”)
.attr(“标题”,标题)
.html(“”+content+“

”) .附录(文件正文); } 函数显示对话框(dlg,销毁){ 变量选项={ 身高:140, 莫代尔:对 }; 如果(销毁){ opts.close=函数(事件,用户界面){ $(this.remove(); }; } 对话框(opts); }
使用:

//加载我们的内容
$.ajax({
url:“http://jsbin.com/urebu5",
数据类型:“json”,
成功:功能(数据){
//使用数据的“foo”部分创建对话框
var dlg=createDialog(“Foo数据”,Data.Foo);
//表现出来
showDialog(dlg,true);
},
错误:函数(xhr、statusText、ex){
//使用数据的“foo”部分创建对话框
var dlg=createDialog(
“Foo数据-错误”,
“运行ajax调用时出错”
);
//表现出来
showDialog(dlg,true);
}
});