jQuery 在对话框中动态创建文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1526409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:38:20  来源:igfitidea点击:

Dynamically create text in dialog box

jqueryjquery-uidialog

提问by acedanger

How can I dynamically set the text of the dialog box that I'm opening? I've tried a few different things but they all respond with an empty dialog box.

如何动态设置我打开的对话框的文本?我尝试了几种不同的方法,但它们都以一个空对话框作为响应。

Here is my current try:

这是我目前的尝试:

$('#dialog').text('Click on the link to download the file:
'.data); $('#dialog').dialog("open");

回答by dano

For best practice, try putting a div inside your dialog div and appending text to that instead.

为了获得最佳实践,请尝试在对话框 div 中放置一个 div 并在其上附加文本。

<div id="myDialog"><div id="myDialogText"></div></div>

and then setting the text of the internal Div. This make for better separation, so you have

然后设置内部Div的文本。这有助于更好地分离,所以你有

  • a div for dialog manipulation
  • and a div for text display
  • 用于对话框操作的 div
  • 和一个用于文本显示的 div

You can then set the text with

然后你可以设置文本

jQuery("#myDialogText").text("your text here");

回答by Chad Kuehn

Here is an alternative way of creating dialogs on the fly and setting their messages dynamically:

这是动态创建对话框并动态设置其消息的另一种方法:

$('<div></div>').dialog({
    modal: true,
    title: "Confirmation",
    open: function() {
      var markup = 'Hello World';
      $(this).html(markup);
    },
    buttons: {
      Ok: function() {
        $( this ).dialog( "close" );
      }
    }   });  //end confirm dialog

See it in action: http://jsfiddle.net/DYbwb/

看看它在行动:http: //jsfiddle.net/DYbwb/

回答by Karmic Coder

Use the plus symbol to concatenate strings:

使用加号连接字符串:

$('#dialog').text('Click on the link to download the file:
' + data);
$('#dialog').dialog("open");

回答by Scott C Wilson

Here's an example showing dynamic text in a jQueryui dialog box. The text is from an ajax response. The message is shown below (larger than it appears!).enter image description here

这是在 jQueryui 对话框中显示动态文本的示例。文本来自 ajax 响应。该消息如下所示(比看起来大!)。在此处输入图片说明

$(".info").click(function(event){
    event.preventDefault();
    $id = $(this).attr("id");
    $.ajax({
           type: "POST",
           url: 'my_code.php',
           data: {query:"info", rowid: $id},
           success: function(data) {
                try {
                   obj = JSON.parse(data);
                   var str = '';
                   for (var i in obj) {
                       str += i + ":" + obj[i] + "<br />";
                       if (i == "id") str += "<hr />";
                   }
                   $("#dialog-1").html(str).dialog();
                   $("#dialog-1").dialog('open');
                } catch (e) {}
           }
        });
});

回答by idrumgood

dialog("open");isn't a valid jquery UI method. (And what Mike said about concatenating with +instead of .

dialog("open");不是有效的 jquery UI 方法。(迈克所说的连接+而不是.

Check the documentation.

检查文档