jQuery添加了多个文本框值

Adi*_*Adi 3 jquery sum

我有一个新任务要做,我不确定使用jQuery处理它的最佳方法.

基本上,客户需要"x"数量的文本框(数量),每个文本框都有一个加号/减号按钮/图标,当您点击每一个时,所有相关字段都会更新.

例如(如果页面中有3个选项)

qty [0] + -
qty [0] + -
qty [0] + -

以上所有[0]的总和

我经常使用jQuery,但我不知道从哪里开始.

任何建议将不胜感激.

阿迪.

Pat*_*Pat 7

如果将CSS类添加到需要求和的所有输入字段,则可以将更改事件处理程序附加到它们所有负责更新总计的处理:

$('input.qty').change(function() {       
    // Loop through all input's and re-calculate the total
    var total = 0;
    $('input.qty').each(function(){
        total += parseInt(this.value);
    });

    // Update the total
    $('#total').val(total);
);
Run Code Online (Sandbox Code Playgroud)

你可以在这里看到它.

笔记

  1. 我遗漏了+和 - 按钮.这些可能只是根据需要递增/递减其相关输入值的链接.
  2. 更新总数用于parseInt从字符串转换为int.您应该确保输入的值是有效整数.