Javascript 如何使用jQuery计算表单输入?

Javascript 如何使用jQuery计算表单输入?,javascript,jquery,Javascript,Jquery,我想使用jquerykeyup函数进行计算 Total Price = Quality x Unit Cost Price Total Price = Quality x Unit Selling Price jQuery: $('input').keyup(function(){ //calculate }); <div class="price_table"> <div class="row"> &

我想使用jquerykeyup函数进行计算

Total Price = Quality x Unit Cost Price

Total Price = Quality x Unit Selling Price
jQuery:

$('input').keyup(function(){
        //calculate
    });
<div class="price_table">
        <div class="row">
            <div class="span5 offset1"><h1>Price Breakdown<h1></div>
            <div class="span4 offset1"><input type="button" id="add_price" class="btn btn-primary" value="Add Price" style="float:right;margin:30px 0px 5px 0px;"/></div>
        </div>
        <div class="row">
            <div class="span10 offset1">
        <table class="addcost_table table tablesorter">
            <thead>
                <tr class="header_row">
                    <th>Item Group</th>
                    <th>Name</th>
                    <th>Code</th>
                    <th>Quantity</th>
                    <th class="cost_price">Unit Cost Price</th>
                    <th class="selling_price">Unit Selling Price</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                   <td><?= form_input('items[0][sub_header]', 'Hotel') ?></td>
                   <td><?= form_input('items[0][name]', 'Twin Room') ?></td>
                   <td><?= form_input('items[0][price_type]', 'TWN') ?></td>
                   <td><?= form_input('items[0][quantity]', '5') ?> </td>
                   <td class="cost_price"><?= form_input('items[0][cost]', '100') ?> </td>
                   <td class="selling_price"><?= form_input('items[0][price]', '120') ?> </td>
                </tr>
                <tr>
                   <td><?= form_input('items[1][sub_header]', 'Hotel') ?></td>
                   <td><?= form_input('items[1][name]', 'Single Room') ?></td>
                   <td><?= form_input('items[1][price_type]', 'SGL') ?></td>
                   <td><?= form_input('items[1][quantity]', '1') ?> </td>
                   <td class="cost_price"><?= form_input('items[1][cost]', '80') ?> </td>
                   <td class="selling_price"><?= form_input('items[1][price]', '100') ?> </td>
                </tr>
                <tr>
                   <td><?= form_input('items[2][sub_header]', 'Meals') ?></td>
                   <td><?= form_input('items[2][name]', 'Buffet Breakfast') ?></td>
                   <td><?= form_input('items[2][price_type]', 'BRE') ?></td>
                   <td><?= form_input('items[2][quantity]', '2') ?> </td>
                   <td class="cost_price"><?= form_input('items[2][cost]', '10') ?> </td>
                   <td class="selling_price"><?= form_input('items[2][price]', '10') ?> </td>
                </tr>
                <tr>
                   <td  colspan="4" style="text-align:right;margin-right:10px;"><b><span style="margin-right:20px;">Total Price X Qty</span></b></td>
                   <td class="cost_price"><?= form_input('items[3][cost]', '600') ?></td>
                   <td class="selling_price"><?= form_input('items[3][cost]', '500') ?></td>

                </tr>
            </tbody>
        </table>
        </div>
        </div>

        <div class="row">
            <div class="span11 offset1">
             <?= form_submit(array('class' => 'btn btn-primary'), 'Submit') ?>
            </div>  
        </div>
    </div>
问题是:单击AddPrice按钮时,会添加新行。如何调用新的行id

HTML:

$('input').keyup(function(){
        //calculate
    });
<div class="price_table">
        <div class="row">
            <div class="span5 offset1"><h1>Price Breakdown<h1></div>
            <div class="span4 offset1"><input type="button" id="add_price" class="btn btn-primary" value="Add Price" style="float:right;margin:30px 0px 5px 0px;"/></div>
        </div>
        <div class="row">
            <div class="span10 offset1">
        <table class="addcost_table table tablesorter">
            <thead>
                <tr class="header_row">
                    <th>Item Group</th>
                    <th>Name</th>
                    <th>Code</th>
                    <th>Quantity</th>
                    <th class="cost_price">Unit Cost Price</th>
                    <th class="selling_price">Unit Selling Price</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                   <td><?= form_input('items[0][sub_header]', 'Hotel') ?></td>
                   <td><?= form_input('items[0][name]', 'Twin Room') ?></td>
                   <td><?= form_input('items[0][price_type]', 'TWN') ?></td>
                   <td><?= form_input('items[0][quantity]', '5') ?> </td>
                   <td class="cost_price"><?= form_input('items[0][cost]', '100') ?> </td>
                   <td class="selling_price"><?= form_input('items[0][price]', '120') ?> </td>
                </tr>
                <tr>
                   <td><?= form_input('items[1][sub_header]', 'Hotel') ?></td>
                   <td><?= form_input('items[1][name]', 'Single Room') ?></td>
                   <td><?= form_input('items[1][price_type]', 'SGL') ?></td>
                   <td><?= form_input('items[1][quantity]', '1') ?> </td>
                   <td class="cost_price"><?= form_input('items[1][cost]', '80') ?> </td>
                   <td class="selling_price"><?= form_input('items[1][price]', '100') ?> </td>
                </tr>
                <tr>
                   <td><?= form_input('items[2][sub_header]', 'Meals') ?></td>
                   <td><?= form_input('items[2][name]', 'Buffet Breakfast') ?></td>
                   <td><?= form_input('items[2][price_type]', 'BRE') ?></td>
                   <td><?= form_input('items[2][quantity]', '2') ?> </td>
                   <td class="cost_price"><?= form_input('items[2][cost]', '10') ?> </td>
                   <td class="selling_price"><?= form_input('items[2][price]', '10') ?> </td>
                </tr>
                <tr>
                   <td  colspan="4" style="text-align:right;margin-right:10px;"><b><span style="margin-right:20px;">Total Price X Qty</span></b></td>
                   <td class="cost_price"><?= form_input('items[3][cost]', '600') ?></td>
                   <td class="selling_price"><?= form_input('items[3][cost]', '500') ?></td>

                </tr>
            </tbody>
        </table>
        </div>
        </div>

        <div class="row">
            <div class="span11 offset1">
             <?= form_submit(array('class' => 'btn btn-primary'), 'Submit') ?>
            </div>  
        </div>
    </div>

价格细目
项目组
名称
代码
量
单位成本价
单位售价
总价X数量

非常感谢我的朋友。

要将侦听器注册到将来的元素(即,在设置处理程序后将创建的元素),请使用on(),并使用第二个参数作为选择器:

$(document).on('keyup', 'input', function(){
    //calculate
});

这表示每当发生键控事件时,检查它是否来自输入,如果是,则调用我的函数

将侦听器注册到未来的元素(即,在设置处理程序后将创建的元素),使用on(),并使用第二个参数作为选择器:

$(document).on('keyup', 'input', function(){
    //calculate
});
$(document).ready(function (){

    $('input').keyup(function(){
        //calculate
        var costpriceSum = 0;
        var selling_priceSum = 0;
        $.find(".cost_price").each(function (){
            costpriceSum += $(this).value;
        });
        $.find(".selling_price").each(function (){
            selling_priceSum += $(this).value;
        });

        //Set costpriceSum
        //Set selling_priceSum

    });

});

这表示每当发生键控事件时,检查它是否来自输入,如果是,则调用我的函数

将侦听器注册到未来的元素(即,在设置处理程序后将创建的元素),使用on(),并使用第二个参数作为选择器:

$(document).on('keyup', 'input', function(){
    //calculate
});
$(document).ready(function (){

    $('input').keyup(function(){
        //calculate
        var costpriceSum = 0;
        var selling_priceSum = 0;
        $.find(".cost_price").each(function (){
            costpriceSum += $(this).value;
        });
        $.find(".selling_price").each(function (){
            selling_priceSum += $(this).value;
        });

        //Set costpriceSum
        //Set selling_priceSum

    });

});

这表示每当发生键控事件时,检查它是否来自输入,如果是,则调用我的函数

将侦听器注册到未来的元素(即,在设置处理程序后将创建的元素),使用on(),并使用第二个参数作为选择器:

$(document).on('keyup', 'input', function(){
    //calculate
});
$(document).ready(function (){

    $('input').keyup(function(){
        //calculate
        var costpriceSum = 0;
        var selling_priceSum = 0;
        $.find(".cost_price").each(function (){
            costpriceSum += $(this).value;
        });
        $.find(".selling_price").each(function (){
            selling_priceSum += $(this).value;
        });

        //Set costpriceSum
        //Set selling_priceSum

    });

});

这意味着,每当发生键控事件时,检查它是否来自输入,如果是,则调用我的函数

,您可能希望为每个输入元素指定一个自己的类,以便可以轻松地对其进行循环

$(document).ready(function (){

    $('input').keyup(function(){
        //calculate
        var costpriceSum = 0;
        var selling_priceSum = 0;
        $.find(".cost_price").each(function (){
            costpriceSum += $(this).value;
        });
        $.find(".selling_price").each(function (){
            selling_priceSum += $(this).value;
        });

        //Set costpriceSum
        //Set selling_priceSum

    });

});
// Enclosed in an anonymous func, is for namespacing, global variables, performance etc.
// See: http://stackoverflow.com/questions/2421911/what-is-the-purpose-of-wrapping-whole-javascript-files-in-anonymous-functions-li
(function(window, document, $, undefined) {
    var totalSelling = calculateTotal('.selling_price_element');
    var totalCost = calculateTotal('.cost_price_element');

    function calculateTotal(element)
    {
        // Start at 0;
        var total = 0;

        // Loop over each element
        $(element).each(function() {

            var element = $(this),
                price = element.val(); // Get the value

            price = parseFloat(price); // Convert the input to a workable float.

            // You might want to do some rounding, validation etc.. here.

            total = total + price; 

        });

        // return the total, round to two decimals
        return total.toFixed(2);
    }
})(this, document, jQuery);

您可能希望为每个输入元素提供一个自己的类,以便可以轻松地对其进行循环

// Enclosed in an anonymous func, is for namespacing, global variables, performance etc.
// See: http://stackoverflow.com/questions/2421911/what-is-the-purpose-of-wrapping-whole-javascript-files-in-anonymous-functions-li
(function(window, document, $, undefined) {
    var totalSelling = calculateTotal('.selling_price_element');
    var totalCost = calculateTotal('.cost_price_element');

    function calculateTotal(element)
    {
        // Start at 0;
        var total = 0;

        // Loop over each element
        $(element).each(function() {

            var element = $(this),
                price = element.val(); // Get the value

            price = parseFloat(price); // Convert the input to a workable float.

            // You might want to do some rounding, validation etc.. here.

            total = total + price; 

        });

        // return the total, round to two decimals
        return total.toFixed(2);
    }
})(this, document, jQuery);

您可能希望为每个输入元素提供一个自己的类,以便可以轻松地对其进行循环

// Enclosed in an anonymous func, is for namespacing, global variables, performance etc.
// See: http://stackoverflow.com/questions/2421911/what-is-the-purpose-of-wrapping-whole-javascript-files-in-anonymous-functions-li
(function(window, document, $, undefined) {
    var totalSelling = calculateTotal('.selling_price_element');
    var totalCost = calculateTotal('.cost_price_element');

    function calculateTotal(element)
    {
        // Start at 0;
        var total = 0;

        // Loop over each element
        $(element).each(function() {

            var element = $(this),
                price = element.val(); // Get the value

            price = parseFloat(price); // Convert the input to a workable float.

            // You might want to do some rounding, validation etc.. here.

            total = total + price; 

        });

        // return the total, round to two decimals
        return total.toFixed(2);
    }
})(this, document, jQuery);

您可能希望为每个输入元素提供一个自己的类,以便可以轻松地对其进行循环

// Enclosed in an anonymous func, is for namespacing, global variables, performance etc.
// See: http://stackoverflow.com/questions/2421911/what-is-the-purpose-of-wrapping-whole-javascript-files-in-anonymous-functions-li
(function(window, document, $, undefined) {
    var totalSelling = calculateTotal('.selling_price_element');
    var totalCost = calculateTotal('.cost_price_element');

    function calculateTotal(element)
    {
        // Start at 0;
        var total = 0;

        // Loop over each element
        $(element).each(function() {

            var element = $(this),
                price = element.val(); // Get the value

            price = parseFloat(price); // Convert the input to a workable float.

            // You might want to do some rounding, validation etc.. here.

            total = total + price; 

        });

        // return the total, round to two decimals
        return total.toFixed(2);
    }
})(this, document, jQuery);
工作小提琴:

我试着让你说的桌子

$('input').keyup( function(self) {
  var buyCostAcc = 0;
  var sellCostAcc = 0;
  $($($($(this).parent().parent().parent()).children()).slice(1, -1)).each(function(self){
    var quantity = $($(this).children()[3]).children().val();
    var buyCost = $($(this).children()[4]).children().val();
    var sellCost = $($(this).children()[5]).children().val();
    buyCostAcc += quantity * buyCost;
    sellCostAcc += quantity * sellCost;
  });
  $($($($($($(this).parent().parent().parent()).children()).slice(-1)).children()[2]).children()).val(buyCostAcc);
  $($($($($($(this).parent().parent().parent()).children()).slice(-1)).children()[3]).children()).val(sellCostAcc);
});
您可以通过分配这个大的parent()来补充一点js代码。parent。。。变量。您无法完成所有工作:)

更新

在这里:

注意,我已经删除了
thead
tbody
标记

这里的技巧是从导致键向上事件的输入DOM节点向上移动,直到到达表节点。一旦你找到了表格,你可以和孩子们一起玩,得到每一行,然后同样得到单元格(以及单元格内的输入)。slice jQuery的方法非常酷,您有一个广泛的解释。JS控制台和调试器对于此类DOM“技巧”非常有用(几乎是强制性的)。

工作提琴:

我试着让你说的桌子

$('input').keyup( function(self) {
  var buyCostAcc = 0;
  var sellCostAcc = 0;
  $($($($(this).parent().parent().parent()).children()).slice(1, -1)).each(function(self){
    var quantity = $($(this).children()[3]).children().val();
    var buyCost = $($(this).children()[4]).children().val();
    var sellCost = $($(this).children()[5]).children().val();
    buyCostAcc += quantity * buyCost;
    sellCostAcc += quantity * sellCost;
  });
  $($($($($($(this).parent().parent().parent()).children()).slice(-1)).children()[2]).children()).val(buyCostAcc);
  $($($($($($(this).parent().parent().parent()).children()).slice(-1)).children()[3]).children()).val(sellCostAcc);
});
您可以通过分配这个大的parent()来补充一点js代码。parent。。。变量。您无法完成所有工作:)

更新

在这里:

注意,我已经删除了
thead
tbody
标记

这里的技巧是从导致键向上事件的输入DOM节点向上移动,直到到达表节点。一旦你找到了表格,你可以和孩子们一起玩,得到每一行,然后同样得到单元格(以及单元格内的输入)。slice jQuery的方法非常酷,您有一个广泛的解释。JS控制台和调试器对于此类DOM“技巧”非常有用(几乎是强制性的)。

工作提琴:

我试着让你说的桌子

$('input').keyup( function(self) {
  var buyCostAcc = 0;
  var sellCostAcc = 0;
  $($($($(this).parent().parent().parent()).children()).slice(1, -1)).each(function(self){
    var quantity = $($(this).children()[3]).children().val();
    var buyCost = $($(this).children()[4]).children().val();
    var sellCost = $($(this).children()[5]).children().val();
    buyCostAcc += quantity * buyCost;
    sellCostAcc += quantity * sellCost;
  });
  $($($($($($(this).parent().parent().parent()).children()).slice(-1)).children()[2]).children()).val(buyCostAcc);
  $($($($($($(this).parent().parent().parent()).children()).slice(-1)).children()[3]).children()).val(sellCostAcc);
});
您可以通过分配这个大的parent()来补充一点js代码。parent。。。变量。您无法完成所有工作:)

更新

在这里:

注意,我已经删除了
thead
tbody
标记

这里的技巧是从导致键向上事件的输入DOM节点向上移动,直到到达表节点。一旦你找到了表格,你可以和孩子们一起玩,得到每一行,然后同样得到单元格(以及单元格内的输入)。slice jQuery的方法非常酷,您有一个广泛的解释。JS控制台和调试器对于此类DOM“技巧”非常有用(几乎是强制性的)。

工作提琴:

我试着让你说的桌子

$('input').keyup( function(self) {
  var buyCostAcc = 0;
  var sellCostAcc = 0;
  $($($($(this).parent().parent().parent()).children()).slice(1, -1)).each(function(self){
    var quantity = $($(this).children()[3]).children().val();
    var buyCost = $($(this).children()[4]).children().val();
    var sellCost = $($(this).children()[5]).children().val();
    buyCostAcc += quantity * buyCost;
    sellCostAcc += quantity * sellCost;
  });
  $($($($($($(this).parent().parent().parent()).children()).slice(-1)).children()[2]).children()).val(buyCostAcc);
  $($($($($($(this).parent().parent().parent()).children()).slice(-1)).children()[3]).children()).val(sellCostAcc);
});
您可以通过分配这个大的parent()来补充一点js代码。parent。。。变量。您无法完成所有工作:)

更新

在这里:

注意,我已经删除了
thead
tbody
标记

这里的技巧是从导致键向上事件的输入DOM节点向上移动,直到到达表节点。一旦你找到了表格,你可以和孩子们一起玩,得到每一行,然后同样得到单元格(以及单元格内的输入)。slice jQuery的方法非常酷,您有一个广泛的解释。JS控制台和调试器非常有用