输入未触发的jquery .keyup事件

时间:2012-05-15 20:45:22

标签: jquery html

当输入是keyup时,我试图进行简单的加法计算。这是我的HTML:

<tr id="a">
        <td>
            <label>A</label>
        </td>
        <td>
            <label>Results/Action Orientation (asset)</label>
        </td>
        <td class="shade">
            <input id="a_per" type="text" />
        </td>
        <td class="shade">
            <input id="a_pot" type="text" />
        </td>
        <td class="shade">
            <input id="a_tot" class="totals" type="text" disabled="disabled" />
        </td>
</tr>

这是我的keyup事件的jQuery:

$('#a_per').keyup(function() {
    if($('#a_pot').value != "") {
        var tot = this.value + $('#a_pot').value;
        $('#a_tot').value = tot;
    }
});

我会在#a_per或#a_pot keyup时定制它来解雇,但首先我要确保它能够正常工作。

感谢任何帮助。感谢。

3 个答案:

答案 0 :(得分:2)

另一种选择:

$('#a_per,#a_pot').keyup(function() {
  var a_per = +$('#a_per').val() || 0;
  var a_pot = +$('#a_pot').val() || 0;
  $('#a_tot').val(a_per + a_pot);
});​

A working example

答案 1 :(得分:1)

将.value的使用更改为.val()。您还需要将parseFloat或parseInt应用于您的添加函数,否则您将最终连接字符串。包装this也是jQuery标签,如:

$('#a_per').keyup(function() {
    if($('#a_pot').val() != "") {
        var tot = parseFloat($(this).val()) + parseFloat($('#a_pot').val());
        $('#a_tot').val(tot);
    }
});​

答案 2 :(得分:1)

使用val(),而不是值。

$(function(){

   $('#a_per').keyup(function() {
     var item=$(this);

      if(item.val() != "") {
         var tot = parseInt(item.val()) + parseInt($('#a_pot').val());
         $('#a_tot').val(tot);
      }
   });

});

工作样本:http://jsfiddle.net/zzS3U/9/