【问题标题】:Recommended way to obtain user input in chrome extension?在 chrome 扩展中获取用户输入的推荐方法?
【发布时间】:2011-02-08 22:47:09
【问题描述】:

我的 Chrome 扩展程序中有一个设置页面,我想知道获取用户输入的推荐(“最佳”)方式是什么。在我的特殊情况下,我需要提供一组组合选项以及一个按钮。单击按钮时,应执行 javascript 函数。我的理解是,如果我使用 a ,这将发布一个我不想要的请求(它会导致页面闪烁并重新加载)。我希望所有这些都是客户端。这是我到目前为止所拥有的(下)。这怎么能改变?提前致谢。

<form>
    <input type="radio" name="format" value="format1"/>(xxx) xxx-xxxx<br/>
    <input type="radio" name="format" value="format2"/>xxx-xxx-xxxx<br/>
    <input type="radio" name="format" value="format3"/>xxx.xxx.xxxx<br/>
    <input type="submit" value="Convert"/>
</form>

【问题讨论】:

    标签: html google-chrome google-chrome-extension


    【解决方案1】:

    Bob,你可以用 JavaScript 做任何事情。由于这是一个 Chrome 扩展程序,请随意使用 HTML5。

    <section>
      <input type="radio" name="format" value="format1"/>(xxx) xxx-xxxx<br/>
      <input type="radio" name="format" value="format2"/>xxx-xxx-xxxx<br/>
      <input type="radio" name="format" value="format3"/>xxx.xxx.xxxx<br/>
      <button id="btnConvert">Convert</button>
      <script>
      document.querySelector('#btnConvert').addEventListener('click', function(e) {
        var format = document.querySelector('input[type="radio"]:checked');
        alert( format.value);
      }, false);
      </script>
    </section>
    

    希望有帮助!

    【讨论】:

    • 再次感谢 Mohamed 提供更出色的帮助 :) 实际上我还不熟悉 HTML5(我是 Web 开发的新手)。我只能告诉你,到目前为止,我一直在使用/学习 jQuery。对于按钮回调,我一直在按钮元素中使用 onclick="callback()" 属性。
    • 似乎对你有用,但我不得不将&lt;script&gt;元素中的JS添加到DOMContentLoaded,无论如何这是一个更简洁的解决方案。不确定问题是来自some sort of JS security rules for Chrome extensions 还是什么,但如果这对其他阅读本文的人不起作用,请注意
    猜你喜欢
    • 1970-01-01
    • 2018-03-30
    • 2016-12-17
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 2016-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多