Jquery 在每次按键时都有一个表单提交

Jquery 在每次按键时都有一个表单提交,jquery,Jquery,我有一个货币计算器,需要按钮来转换货币。我想让它在输入任何内容后立即转换(运行表单)。目前,处理按钮单击的jQuery如下所示(这是两个单击处理程序之一): 第一个文本字段是#fromCurrencyValue,第二个文本字段是#btcValue 我怎样才能让它以我描述的方式工作 // if you keyup in fromCurrencyValue or btcValue it will trigger the click on convertFromButton $("#fromCurre

我有一个货币计算器,需要按钮来转换货币。我想让它在输入任何内容后立即转换(运行表单)。目前,处理按钮单击的jQuery如下所示(这是两个单击处理程序之一):

第一个文本字段是
#fromCurrencyValue
,第二个文本字段是
#btcValue

我怎样才能让它以我描述的方式工作

// if you keyup in fromCurrencyValue or btcValue it will trigger the click on convertFromButton
$("#fromCurrencyValue, #btcValue").bind("keyup", function() {
  $('#convertFromButton').click();
});
但是请记住,$.post默认是异步的,因此,如果稍后询问的一个请求比另一个请求先得到答复,则会出现不需要的行为

编辑

过帐价值时,请使用以下内容:

var xhr; // make it global

/*
  Your code goes here
*/
if (xhr)
  xhr.abort(); // if there's a xhr request abort it

xhr = $.post(...)
/*
  The rest of your code
*/
所以你不会有不想要的行为:)

我会的

$('#formCurrencyValue, #btcValue').keyup(function() {$('#convertFromButton').click(); });
然后,如果您更改了任何提交逻辑,则所有事件的提交逻辑都将更改


我把它换成了钥匙。这应该适用于每按一个键。

我要说的是,从其他函数/事件显式调用事件处理程序是一种肮脏的做法,应该避免。通过执行以下操作,您可以更加清楚:

    var xhr;
    function refresh() {
        $("#currencySelectValue2").val($("#currencySelect").val());
        $("#btcValueForm").val($("#btcValue").val());
        if(xhr) xhr.abort();
        xhr = $.post("php/convertFromForm.php", $("#convertFromForm").serialize(), function(data){ 
            $('#fromCurrencyValue').removeClass('intra-field-label').val(data);
        });
    }

    //Bind as many things as you want to your new function.  Also easily unit tested!  Yay!
    $('#convertFromButton').click(refresh);
    $('#fromCurrencyValue, #btcValue').keyup(refresh)

你错过了一次机会;在队伍的最后。太棒了,这很好用。唯一的一点——当我退格时,表单不会提交。我是否可以在其中包含backspace(并允许粘贴数字)?人们还应该考虑这样一个事实,Jquery帖子是异步的,早期调用的帖子有时会覆盖后期调用的帖子。
    var xhr;
    function refresh() {
        $("#currencySelectValue2").val($("#currencySelect").val());
        $("#btcValueForm").val($("#btcValue").val());
        if(xhr) xhr.abort();
        xhr = $.post("php/convertFromForm.php", $("#convertFromForm").serialize(), function(data){ 
            $('#fromCurrencyValue').removeClass('intra-field-label').val(data);
        });
    }

    //Bind as many things as you want to your new function.  Also easily unit tested!  Yay!
    $('#convertFromButton').click(refresh);
    $('#fromCurrencyValue, #btcValue').keyup(refresh)