表单字段值的总和在jquery添加的字段中不起作用

时间:2014-08-02 09:18:19

标签: jquery add html-form

我有一个表单,它正在第一行添加值, 但是点击“添加更多”后,它会停止在第二行添加值。按钮。 其次,当我在<时使用方括号时,它也不会添加。输入名称=" sum []"类="总"值="" />在数组中得到它的值。

    <div class="my-form">
    <form name="form" >
    <p class="text-box">

     <input type="text" name="size[]" class="add" value="" id="box1"/>  
      <input type="text" name="qty[]"  class="add"   value="" id="box2"/> 

     Total:   <input name="sum" class="total" value=""/>


            <a class="add-box" href="#">Add More</a>
                </p>
                <p><input type="submit" value="Submit" /></p>
    </form>
    </div>

    <!-- **** Script to Add More fields **** -->
    <script type="text/javascript">
    jQuery(document).ready(function($){
        $('.my-form .add-box').click(function(){
            var n = $('.text-box').length + 1;
            if( 10 < n ) {
                alert('Stop it!');
                return false;
            }
            var box_html = $('<p class="text-box"> <input type="text" name="size[]" class="add" value="" id="box1' + n + '" /> <input type="text" name="qty[]" class="add" value="" id="box2' + n + '" /> Total: <input name="sum" class="total" value=""/> <a href="#" class="remove-box">Remove</a></p>');
            jQuery('#my-form').append(box_html);
            box_html.hide();
            $('.my-form p.text-box:last').after(box_html);
            box_html.fadeIn('slow');
            return false;
        });

        $('.my-form').on('click', '.remove-box', function(){
            $(this).parent().css( 'background-color', '#FF6C6C' );
            $(this).parent().fadeOut("slow", function() {
                $(this).remove();
                $('.box-number').each(function(index){
                    $(this).text( index + 1 );
                });
            });
            return false;
        });
    });
    </script>

    <!-- **** Script to Sum field Values **** -->
    <script type="text/javascript">
    $.fn.sumValues = function() {
        var sum = 0; 
        this.each(function() {
            if ( $(this).is(':input') ) {
                var val = $(this).val();
            } else {
                var val = $(this).text();
            }
    document.form.sum.value = sum += parseFloat( ('0' + val).replace(/[^0-9-\.]/g, ''), 10 );
        });
        return sum;
    };

    $(document).ready(function() {
        $('input.add').bind('keyup', function() {
            $('span.total').html( $('input.add').sumValues() );
        });
    });
    </script>

这里可以看到工作示例 - Click here

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

问题:点击“添加更多”按钮后,它会停止在第二行添加值

解决方案:使用事件委派,您可以将函数绑定到动态元素中。

$(document).ready(function() {
    $('form').bind('keyup', 'input.add', function() {
        $(this).parent().find('span.total').html($(this).parent().find('input.add').sumValues());
    }); 
});

问题:使用方括号时不要添加

解决方案:您不能只在代码的每一行添加输入,如果您想使用单独的行,则需要在每行的末尾添加\ .. < / p>

var box_html = $('<p class="text-box"> <input type="text" name="size[]" class="add" value="" id="box1' + n + '" /> \
<input type="text" name="qty[]" class="add" value="" id="box2' + n + '" /> \
Total: <input name="sum[]" class="total" value=""/> \
<a href="#" class="remove-box">Remove</a></p>');

答案 1 :(得分:-1)

I have created the fiddle for the same. 
Please check this, may help you.

http://jsfiddle.net/sandeep_pal/Ma5dK/