调用Jquery对话框并传递消息的字符串

时间:2012-05-19 18:40:39

标签: javascript jquery

我想调用JavaScript函数并将字符串传递给它。我试过这种方式:

function dialog(a){              
    // Dialog           
    $(a).dialog({        
        width: 600,
        buttons: {
            "Ok": function() { 
                $(this).dialog("close"); 
            }, 
            "Cancel": function(event) { 
                $(this).dialog("close");
                event.preventDefault();
            } 
        }
    });

这是JSF按钮:

<h:commandButton value="Delete" action="#{SessionsController.deleteSelectedIDs}" 
                    onclick="dialog('Dialog Test')">
    <f:ajax render="@form" execute="@form"></f:ajax>
</h:commandButton>

当我尝试使用这种方式onclick="dialog('Dialog Test')"传递字符串时,它无效。我怎么解决这个问题?

祝福

2 个答案:

答案 0 :(得分:2)

你可能想要这样的东西:

function dialog(a) {
    // Dialog           
    $("<div />", { text: a }).dialog({
        width: 600,
        buttons: {
            "Ok": function() {
                $(this).dialog("close");
            },
            "Cancel": function(event) {
                $(this).dialog("close");
                event.preventDefault();
            }
        }
    });
}

在<{em>}上调用<div />之前,将文本包含在dialog中,其中包含对话框文本。以前你传递jQuery是一个无效的选择器,这就是没有弹出的原因。

示例: http://jsfiddle.net/4extL/2/

答案 1 :(得分:1)

将字符串传递给dialog()函数可能正常工作。结果可能不起作用:

$(a)

变为:

$('Dialog Test')

因为它告诉jQuery查找<Test>对象中包含的<Dialog>对象的CSS选择器,这根本不是你的意思。