【问题标题】:Buttons that change units of measurement dynamically on page在页面上动态更改测量单位的按钮
【发布时间】:2012-01-17 10:02:55
【问题描述】:

我正在尝试实现一个 JavaScript 函数,该函数将修改输出到字段中的结果值。

输出到字段的结果示例为 千字节 和英国 磅。

但是,我在表单中包含了按钮,我将使用这些按钮来修改结果以将它们放入

千兆字节、兆字节和便士

请看下面的截图:

伪代码:

>Button pressed calls function.
>Function identifies what button user pressed
>If the button is related to currency unit then modify global variable **currency**
>If the button is related to data units then modify global variable **data**
>Recalculate all fields using new variable values and output to form.

我正在考虑实现它的方式是,我的 JavaScript 中将有两个全局变量,这将根据按下的按钮而改变。然后它们将用于计算数据和金钱。为什么是全球性的? - 我有单独的 JavaScript 函数来检索值并计算它们。

我需要输入有关如何检测按下哪个按钮的信息?

另外,如果你知道任何类似的 JS 示例,那就太好了。

欢迎任何建议!

【问题讨论】:

    标签: javascript dom-events currency


    【解决方案1】:

    为了知道哪个按钮被按下,为所有按钮添加一个 onclick 事件处理程序和 onclick 事件处理程序函数,您可以检测每个按钮的值,从中您可以识别按钮点击。 例如

    <input type="button" value="gb" onclick="identifyButton(this)" />
    
    function identifyButton(obj)
    {
       alert(obj.value);
    }
    

    希望对你有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-13
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多