将参数传递给jquery ui对话框

Mar*_*arc 5 dialog jquery-ui this

我用.data这个来传递调用对话框的文本框的id

$("#<%=txtDirProprio.ClientID%>").focus(function() 
{
         $("#<%=dialog.ClientID%>").dialog( "open" ).data("id","#<%=txtDirProprio.ClientID%>");
         return false;
});
Run Code Online (Sandbox Code Playgroud)

这是对话框的代码

 $("#<%=dialog.ClientID%>").dialog({
                autoOpen: false,
                show: "blind",
                hide: "explode",
                width: 800,
                height:200,
                modal: true,
                buttons: 
                {
                    "Ajouter": function() {
                        $( this ).dialog( "close" );
                        StringBuilderDir($( this ).data("id"));
                    },
                    "Vider": function() {
                        $( this ).dialog( "close" );
                        $( $( this ).data("id") ).val("")
                    },
                    "Canceler": function() {
                        $( this ).dialog( "close" );
                    }
                },
                open: function() 
                { 
                    var dir = $( $( this ).data("id") ).val().split("-");
                    if(dir[0] != "")
                    {
                        $("#<%=dd_dialog_directionvp.ClientID%> option").each(function(index) 
                        {
                            if ($("#<%=dd_dialog_directionvp.ClientID()%> option")[index].text == dir[0]) 
                            {
                                $("#<%=dd_dialog_directionvp.ClientID()%>  option")[index].selected = true;
                            }
                        })
                     }
                 }
                 });
Run Code Online (Sandbox Code Playgroud)

所以$ ( this ).data("id")返回文本框的id.除了open函数外,它工作正常.id未定义

为什么它适用于按钮的功能,而不适用于打开功能.它看起来不一样'这个'

谢谢

Bri*_*ian 9

$("#<%=txtDirProprio.ClientID%>").focus(function() 
{
         $("#<%=dialog.ClientID%>").data("id","#<%=txtDirProprio.ClientID%>").dialog( "open" );
         return false;
});
Run Code Online (Sandbox Code Playgroud)

在呼叫之前必须先设置数据 .dialog('open');

  • 它没有工作,因为当你调用.dialog('open')打开对话框并执行open函数时,它返回jQuery并调用下一个方法.data(),设置数据.因为open函数在设置数据之前执行,它在打开时不可用;) (2认同)