0

我有一个带有动态输入字段的表单,用于插入数字,其中第一个输入是静态的,其他 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() 的东西?

4

1 回答 1

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;
    })
于 2013-06-06T12:29:40.437 回答