jQuery UI 1.7 - 在多个脚本中获取标题选项

时间:2014-02-10 19:27:11

标签: javascript jquery-ui jquery-ui-dialog

除了适当的抽象之外,我如何在多个脚本中获取jQuery对话框的'title'选项?

例如,下面我有2个脚本,其中第一个将var作为字符串返回,而另一个不将变量作为字符串返回:

<script>
$(function() {

    $( ".scheduleDiv" ).dialog({
        autoOpen: false,
        height: 300,
        width: 350,
        modal: true,
        Cancel: function() {
            $( this ).dialog( "close" );
        }
    });

    $( ".device-modal" )
    .click(function() {
        $( ".scheduleDiv" ).dialog({"title": $(this).attr('title')});
        alert($( ".scheduleDiv" ).dialog('option', 'title'));
        var single = $(this).dialog('option', 'title');         //works as expected
        $( ".scheduleDiv" ).dialog( "open" );
        return false;
    });
});
</script>

<script>
$(document).ready(function() {
    $('.locks').hide();
    $('.device_groups').hide();

    var single = $(this).dialog('option', 'title');              //is undefined...

    if (single == "Add Single Lock") {
        $('.locks').show();
        //$('.locks select').removeAttr('disabled');
    }
    else {
        $('.device_groups').show();
        //$('.device_groups select').removeAttr('disabled');
    }

});
</script>

那么,如何设置'single'在第二个脚本中将标题作为字符串返回?

非常感谢

1 个答案:

答案 0 :(得分:0)

this的上下文可能不正确。处理$(document).ready()事件时,this是文档。您需要手动抓取对话框窗口,然后获取标题:

$('.scheduleDiv').dialog('option', 'title');

用于获取标题的代码正在ondocumentready上启动,但您的初始化实际上并未设置标题。在单击“deviceModal”之前,您不会设置标题。此时,记录标题的代码将不会被重新触发,因为文档只加载一次。