Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何获得双向单位/货币转换器?_Javascript_Html_Currency - Fatal编程技术网

Javascript 如何获得双向单位/货币转换器?

Javascript 如何获得双向单位/货币转换器?,javascript,html,currency,Javascript,Html,Currency,我正在尝试构建一个非常简单的双向货币转换器 我目前有以下HTML: <input id="amount" name="amount" type="text" value="0" /> <input id="result" name="discount" type="text" value="0" /> 我希望转换器能够双向工作,以便用户可以在任一字段中输入金额。您要查找的是事件 因此,您的代码如下所示: $("#amount").change(func

我正在尝试构建一个非常简单的双向货币转换器

我目前有以下HTML:

    <input id="amount" name="amount" type="text" value="0" />

    <input id="result" name="discount" type="text" value="0" />

我希望转换器能够双向工作,以便用户可以在任一字段中输入金额。

您要查找的是事件

因此,您的代码如下所示:

$("#amount").change(function() {
    //do your conversions here...
    } );
然后对另一个
#result
字段执行相同的操作

有一个触发器。这样,如果触发了#金额框,您将在#结果框中获得结果,反之亦然

<input id="amount" name="amount" type="text" value="0" />
<input id="result" name="discount" type="text" value="0" />

您仍然可以有一个事件处理程序,但使用
change
event并使用已更改输入的id检查您应该更新的输入

$('input').on("change",function () {
  if (this.id === 'result') {
    $("#amount").val(parseInt($("#result").val(), 10) /  520);
  } else if (this.id === 'amount') {
    $("#result").val(parseInt($("#amount").val(), 10) *  520);
  }
});

@rory在这种情况下,请使用按键而不是向下键。或者正如你所说,onchange(但它不是“实时”转换的)@roborads-啊,是的,你是对的。从Jquery页面。。。对于选择框、复选框和单选按钮,当用户用鼠标进行选择时,事件会立即触发,但对于其他元素类型,事件会延迟到元素失去焦点为止
$("#amount").keyup(function(){
    var calculated = $(this).val() * 2;
    $("#result").val(calculated);
});

$("#result").keyup(function(){
    var calculated = $(this).val() / 2;
    $("#amount").val(calculated);
});
$('input').on("change",function () {
  if (this.id === 'result') {
    $("#amount").val(parseInt($("#result").val(), 10) /  520);
  } else if (this.id === 'amount') {
    $("#result").val(parseInt($("#amount").val(), 10) *  520);
  }
});