【问题标题】:Why does this Code Magazine sample fail in Chrome (only)?为什么此 Code Magazine 示例在 Chrome 中失败(仅)?
【发布时间】:2016-08-11 20:06:07
【问题描述】:

我从this Code Magazine article 下载了示例代码。它是在 html 表单中单击提交按钮时的进度指示器。它适用于 Firefox 和 IE,但在 Chrome 中,提交永远不会完成。按钮文本从 Save 更改为 Saving... 并且永远不会恢复为 Save。由于某种原因,该表格从未发布过。这是提交按钮:

<button type="submit" id="submitButton" class="btn btn-primary" onclick="return DisplayProgressMessage(this, 'Saving...');">
    Save
</button>

这是脚本:

<script>
    function DisplayProgressMessage(ctl, msg) {
        $(ctl).prop("disabled", true);
        $(ctl).text(msg);

        return true;
}

【问题讨论】:

    标签: javascript c# asp.net-mvc google-chrome


    【解决方案1】:

    这可能与Content Security Policy for Google Chrome(如果您启用了扩展程序)有关,它可能会禁用内联事件,例如onclick

    除此之外,代码不会将元素恢复为 Save,因为您没有告诉它这样做。

    方法分解:

    $(ctl) 将引用按钮

    • 你禁用它
    • 您将文本设置为“正在保存”
    • 你从 功能

    您没有告诉它将文本重置为“保存”

    如果更改代码结构,它的行为完全相同

    var $button = $("#submitButton");
    
    $button.on("click", function() {
         DisplayProgressMessage(this, "saving...");
    });
    
    function DisplayProgressMessage(ctl, msg) {
       $(ctl).prop("disabled", true);
       $(ctl).text(msg);
    
       return true;
    }
    

    https://jsfiddle.net/xzq701r9/

    【讨论】:

    • 谢谢。我将研究内容安全策略。我相信我已经准确地复制了文章中的代码。根据您的描述和 jsfiddle,我现在很惊讶它可以在 Firefox 和 Chrome 中运行。
    • 我已经澄清了我的问题,表明提交按钮在表单中。帖子后应显示“保存”一词。我认为是内容安全政策导致了问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多