为插件的属性分配可验证的值

Assigning a value of veriable to property of a plugin

本文关键字:可验证 分配 属性 插件      更新时间:2023-09-26

我是Js Newbie。

我正在尝试使用以下方式设置 Plupload 的属性。

$(function () {
    var myUrl = "https://" + $('input#hfBucketName').val() + $('input#hfEndPoint').val();
    $("#uploaderdiv").plupload({
        runtimes: 'html5,flash,silverlight',
        url: myUrl,
        //Removed for simplicity
    });
});

这在尝试上传文件时给了我Upload URL might be wrong or doesn't exist.错误。

假设我无法使用计算变量分配插件的 url 属性。

在Chrome控制台上,当我在页面完成加载后键入myUrl时,我得到ReferenceError: myUrl is not defined

但是,当我键入(在控制台上)var myUrl = "https://" + $('input#hfBucketName').val() + $('input#hfEndPoint').val();然后myUrl时,它给了我期望的值。

如何将myUrl分配给插件的属性url?我错在哪里?

编辑

不好意思。我在将我的代码包含在问题中时出错了。我现在已经修复了我的代码块。实际上一切都包裹在$(function () { });内,但仍然不能解决我的问题。

您正在尝试访问可能尚未加载的元素的值。 $(function() {}); 是 document.ready 事件的 jQuery 简写。将 myUrl 变量声明移动到文档就绪函数中应该可以解决您的问题:

$(function () {
    var myUrl = "https://" + $('input#hfBucketName').val() + $('input#hfEndPoint').val();
    $("#uploaderdiv").plupload({
        runtimes: 'html5,flash,silverlight',
        url: myUrl,
        //Removed for simplicity
    });
});