从sum中删除值以及动态字段删除

时间:2013-06-06 12:23:35

标签: jquery dynamic numbers sum

我有一个带有动态输入字段的表单,用于插入数字,其中第一个输入是静态的,另外5个是用脚本动态创建的。最后一个字段是数字之和的连接。

<form class="form-horizontal" id="whereEntry" method='post' action=''>
   <fieldset>

      <div class="control-group">
        <div class="controls controls-row">
            <input type="text" class="span3 register_input" id="main_activity" name="main_activity" placeholder="Company's main activity">
              <input type="text" class="span1 register_input income_count" id="income" name="income" placeholder="% of income">
               </div>
                </div>

<div class="control-group">
<div class="controls controls-row">
    <div id="InputsIncomeWrapper">
    </div>
</div>
<div class="row">
    <input type="text" class="span1 register_input pull-right" id="income_sum"  name="income_sum">
</div>
<div class="row">
    <a href="#" id="AddMoreIncomeBox" class="btn btn-info pull-right"><i class="icon-plus"></i>Add More</a>
</div>
</div>

       </fieldset>
       </form>
总结的

脚本:

//calculation script
var $form = $('#whereEntry'),
$sumDisplay = $('#income_sum');

$form.delegate('.income_count', 'change', function ()
{
var $summands = $form.find('.income_count');
var sum = 0;
$summands.each(function ()
{
    var value = Number($(this).val());
    if (!isNaN(value)) sum += value;
});

$sumDisplay.val(sum);
});
动态字段的

脚本:

 //add dynamic field script
 $(document).ready(function() {

 var MaxInputs       = 5; //maximum input boxes allowed
 var InputsWrapper   = $("#InputsIncomeWrapper"); //Input boxes wrapper ID
 var AddButton       = $("#AddMoreIncomeBox"); //Add button ID

 var x = InputsWrapper.length; //initlal text box count
 var FieldCount=1; //to keep track of text box added

 $(AddButton).click(function (e)  //on add input button click
 {
    if(x <= MaxInputs) //max input box allowed
    {
        FieldCount++; //text box added increment
        //add input box
        $(InputsWrapper).append('\
        <div>\
        <input type="text" class="register_input span3"\
        name="main_activity_'+ FieldCount +'" id="main_activity_'+ FieldCount +'"\
        placeholder="Company´s other activity" style="margin:0px 15px 20px 0px"/>\
        <input type="text" class="span1 register_input income_count" id="income_'+ FieldCount +'"\
        name="income_'+ FieldCount +'" placeholder="% of income"\ style="margin:0px 15px 20px 15px"/>\
        <a href="#" class="removeclass pull-left"><i class="icon-remove icon-remove-addincome"></i></a></div>');
        x++; //text box increment
    }
  return false;
 });

 $("body").on("click",".removeclass", function(e){ //user click on remove text
    if( x > 1 ) {
            $(this).parent('div').remove(); //remove text box
            x--; //decrement textbox
    }
 return false;
}) 

});

这里是关于jsfiddle的示例:http://jsfiddle.net/Uwbe6/2/

现在一切正常,但是例如,当我添加两个动态字段时,我在第一个静态输入30,在第二个动态输入40,在第三个动态输入30。 #income_sum中的值是100.但是如果我改变主意并且“关闭”两个动态字段。问题是我的总和值不会恢复到仅从第一个字段拉出的40。它保持100.有任何非困难的解决方案吗?什么东西与.income_count去除.change上的.change()?

1 个答案:

答案 0 :(得分:1)

在删除元素时触发更改事件:

http://jsfiddle.net/qWhvH/

$("body").on("click", ".removeclass", function (e) { //user click on remove text
        if (x > 1) {
            $(this).parent('div').remove(); //remove text box
            x--; //decrement textbox
        }

        $('.income_count').trigger('change');
        return false;
    })