简体   繁体   English

Javascript动态输入计算

[英]Javascript dynamic inputs calculation

I have a form which is dynamic and it calculates duration time. 我有一个动态的表格,它计算持续时间。 I can insert rows by clicking on Add a new row. 我可以通过单击添加新行来插入行。 My problem starts from the second row which can not be calculated because it is dynamic. 我的问题从无法计算的第二行开始,因为它是动态的。 Could you please help me with that. 你能帮我解决吗?

Thanks 谢谢

 $(document).ready(function() { var max_fields = 10; //maximum input boxes allowed var wrapper = $(".input_fields_wrap"); //Fields wrapper var add_button = $(".add_field_button"); //Add button ID var FieldCount = 1; //to keep track of text box added var x = 1; //initlal text box count $(add_button).click(function(e) { //on add input button click e.preventDefault(); if (x < max_fields) { //max input box allowed x++; //text box increment FieldCount++; wrapper.append('From &rarr; <input type="text" name="fromhours" id="fromhours' + FieldCount + '" onblur="cal()" /> : <input type="text" name="fromminutes" id="fromminutes' + FieldCount + '" onblur="cal()" /> | To &rarr; <input type="text" name="tohours" id="tohours' + FieldCount + '" onblur="cal()" /> : <input type="text" name="tominutes" id="tominutes' + FieldCount + '" onblur="cal()" /> | Result &rarr; <input type="text" name="resulthours" id="resulthours' + FieldCount + '" /> : <input type="text" name="resultminutes" id="resultminutes' + FieldCount + '" /><br /><br />'); //add input box } }); wrapper.on("click", ".remove_field", function(e) { //user click on remove text e.preventDefault(); $(this).parent('div').remove(); x--; }) }); function cal() { var fromhours = parseInt(document.getElementById('fromhours').value) * 60; var fromminutes = parseInt(document.getElementById('fromminutes').value); var tohours = parseInt(document.getElementById('tohours').value) * 60; var tominutes = parseInt(document.getElementById('tominutes').value); var resultfrom = fromhours + fromminutes; var resultto = tohours + tominutes; var result = resultto - resultfrom; var hourresult = parseInt(result / 60); var minutesresult = (result - (hourresult * 60)); document.getElementById('resulthours').value = '0' + hourresult.toFixed(0); document.getElementById('resultminutes').value = ('0' + minutesresult).slice(-2); } 
 input[type=text] { width: 25px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input style="margin-left:28px;" type="image" class="add_field_button" value="Add a new row" /> <br />From &rarr; <input type="text" name="fromhours" id="fromhours" onblur="cal()" />: <input type="text" name="fromminutes" id="fromminutes" onblur="cal()" />| To &rarr; <input type="text" name="tohours" id="tohours" onblur="cal()" />: <input type="text" name="tominutes" id="tominutes" onblur="cal()" />| Result &rarr; <input type="text" name="resulthours" id="resulthours" />: <input type="text" name="resultminutes" id="resultminutes" /> <br /> <br /> <div class="input_fields_wrap"></div> 

My problem is from the second row I can't get my result to work 我的问题是从第二行我无法得到我的结果

You need to refer the current element row id by adding x as follows 您需要通过添加x来引用当前元素行id,如下所示

  wrapper.append('From &rarr; 
<input type="text" name="fromhours" id="fromhours' + FieldCount + '" onblur="cal(x)" /> ...


   function cal(x) {
      var fromhours = parseInt(document.getElementById('fromhours'+x).value) *   60;
   ...

 $(document).ready(function() { var max_fields = 10; //maximum input boxes allowed var wrapper = $(".input_fields_wrap"); //Fields wrapper var add_button = $(".add_field_button"); //Add button ID var FieldCount = 1; //to keep track of text box added var x = 1; //initlal text box count $(add_button).click(function(e) { //on add input button click e.preventDefault(); if (x < max_fields) { //max input box allowed x++; //text box increment FieldCount++; wrapper.append('From &rarr; <input type="text" name="fromhours" id="fromhours' + FieldCount + '" onblur="cal('+FieldCount +')" /> : <input type="text" name="fromminutes" id="fromminutes' + FieldCount + '" onblur="cal('+FieldCount +')" /> | To &rarr; <input type="text" name="tohours" id="tohours' + FieldCount + '" onblur="cal('+FieldCount +')" /> : <input type="text" name="tominutes" id="tominutes' + FieldCount + '" onblur="cal('+FieldCount +')" /> | Result &rarr; <input type="text" name="resulthours" id="resulthours' + FieldCount + '" /> : <input type="text" name="resultminutes" id="resultminutes' + FieldCount + '" /><br /><br />'); //add input box } }); wrapper.on("click", ".remove_field", function(e) { //user click on remove text e.preventDefault(); $(this).parent('div').remove(); x--; }) }); function cal(x) { x=x||""; var fromhours = parseInt(document.getElementById('fromhours'+x).value) * 60; var fromminutes = parseInt(document.getElementById('fromminutes'+x).value); var tohours = parseInt(document.getElementById('tohours'+x).value) * 60; var tominutes = parseInt(document.getElementById('tominutes'+x).value); var resultfrom = fromhours + fromminutes; var resultto = tohours + tominutes; var result = resultto - resultfrom; var hourresult = parseInt(result / 60); var minutesresult = (result - (hourresult * 60)); document.getElementById('resulthours'+x).value = '0' + hourresult.toFixed(0); document.getElementById('resultminutes'+x).value = ('0' + minutesresult).slice(-2); } 
 input[type=text] { width: 25px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input style="margin-left:28px;" type="image" class="add_field_button" value="Add a new row" /> <br />From &rarr; <input type="text" name="fromhours" id="fromhours" onblur="cal()" />: <input type="text" name="fromminutes" id="fromminutes" onblur="cal()" />| To &rarr; <input type="text" name="tohours" id="tohours" onblur="cal()" />: <input type="text" name="tominutes" id="tominutes" onblur="cal()" />| Result &rarr; <input type="text" name="resulthours" id="resulthours" />: <input type="text" name="resultminutes" id="resultminutes" /> <br /> <br /> <div class="input_fields_wrap"></div> 

I am not a JS expert, but could it have something to do with the fact, that you refer to the rows under the same ID attribute? 我不是JS专家,但它可能与事实有关,你引用相同ID属性下的行? As far as I can see you're not specifying to the code which one of the rows to calculate. 据我所知,你没有指定要计算哪一行的代码。

See @Bellash answer for a possible solution 有关可能的解决方案,请参阅@Bellash答案

You forgot to pass the fieldCount number inside the cal() function and concatenate it with the field ids, so the cal() function always used the first row of text fields. 您忘记在cal()函数内传递fieldCount数字并将其与字段ID连接,因此cal()函数始终使用第一行文本字段。 I've corrected your snippet for you. 我已经纠正了你的代码片段。

 $(document).ready(function() { var max_fields = 10; //maximum input boxes allowed var wrapper = $(".input_fields_wrap"); //Fields wrapper var add_button = $(".add_field_button"); //Add button ID var FieldCount = 1; //to keep track of text box added var x = 1; //initlal text box count $(add_button).click(function(e) { //on add input button click e.preventDefault(); if (x < max_fields) { //max input box allowed x++; //text box increment FieldCount++; wrapper.append('From &rarr; <input type="text" name="fromhours" id="fromhours' + FieldCount + '" onblur="cal(' + FieldCount + ')" /> : <input type="text" name="fromminutes" id="fromminutes' + FieldCount + '" onblur="cal(' + FieldCount + ')" /> | To &rarr; <input type="text" name="tohours" id="tohours' + FieldCount + '" onblur="cal(' + FieldCount + ')" /> : <input type="text" name="tominutes" id="tominutes' + FieldCount + '" onblur="cal(' + FieldCount + ')" /> | Result &rarr; <input type="text" name="resulthours" id="resulthours' + FieldCount + '" /> : <input type="text" name="resultminutes" id="resultminutes' + FieldCount + '" /><br /><br />'); //add input box } }); wrapper.on("click", ".remove_field", function(e) { //user click on remove text e.preventDefault(); $(this).parent('div').remove(); x--; }) }); function cal(fieldCount) { console.log(arguments); var fromhours = parseInt(document.getElementById('fromhours' + fieldCount).value) * 60; var fromminutes = parseInt(document.getElementById('fromminutes' + fieldCount).value); var tohours = parseInt(document.getElementById('tohours' + fieldCount).value) * 60; var tominutes = parseInt(document.getElementById('tominutes' + fieldCount).value); var resultfrom = fromhours + fromminutes; var resultto = tohours + tominutes; var result = resultto - resultfrom; var hourresult = parseInt(result / 60); var minutesresult = (result - (hourresult * 60)); document.getElementById('resulthours' + fieldCount).value = '0' + hourresult.toFixed(0); document.getElementById('resultminutes' + fieldCount).value = ('0' + minutesresult).slice(-2); } 
 input[type=text] { width: 25px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input style="margin-left:28px;" type="image" class="add_field_button" value="Add a new row" /> <br />From &rarr; <input type="text" name="fromhours" id="fromhours1" onblur="cal(1)" />: <input type="text" name="fromminutes" id="fromminutes1" onblur="cal(1)" />| To &rarr; <input type="text" name="tohours" id="tohours1" onblur="cal(1)" />: <input type="text" name="tominutes" id="tominutes1" onblur="cal(1)" />| Result &rarr; <input type="text" name="resulthours" id="resulthours1" />: <input type="text" name="resultminutes" id="resultminutes1" /> <br /> <br /> <div class="input_fields_wrap"></div> 

You have to get the unique id and use that in your calculations. 您必须获取唯一ID并在计算中使用它。 In the JS I replaced the function with an on keyup and added a statement to prevent the NaN 在JS中,我用on keyup替换了函数,并添加了一个语句以防止NaN

JS JS

$(document).ready(function() {
  var max_fields = 10; //maximum input boxes allowed
  var wrapper = $(".input_fields_wrap"); //Fields wrapper
  var add_button = $(".add_field_button"); //Add button ID
  var FieldCount = 1; //to keep track of text box added
  var x = 1; //initlal text box count
  $(add_button).click(function(e) { //on add input button click
    e.preventDefault();
    if (x < max_fields) { //max input box allowed
      x++; //text box increment
      FieldCount++;
      wrapper.append('From &rarr; <input type="text" class="cal" name="fromhours" id="fromhours' + FieldCount + '" /> : <input type="text" class="cal" name="fromminutes" id="fromminutes' + FieldCount + '" /> | To &rarr; <input type="text" class="cal" name="tohours" id="tohours' + FieldCount + '" /> : <input type="text" class="cal" name="tominutes" id="tominutes' + FieldCount + '" /> | Result &rarr; <input type="text" class="cal" name="resulthours" id="resulthours' + FieldCount + '" /> : <input type="text" class="cal" name="resultminutes" id="resultminutes' + FieldCount + '" /><br /><br />'); //add input box
    }
  });

  wrapper.on("click", ".remove_field", function(e) { //user click on remove text
    e.preventDefault();
    $(this).parent('div').remove();
    x--;
  })
});

$('body').on('keyup', '.cal', function () {
    var id = $(this).attr('id').substr(-1),
        fromhours = ~~$('#fromhours' + id).val(),
        fromminutes = ~~$('#fromminutes' + id).val(),
        tohours = ~~$('#tohours' + id).val(),
        tominutes = ~~$('#tominutes' + id).val();

    if (fromhours != '' && fromminutes != '' && tohours != '' && tominutes != '') {
        var resultfrom = (fromhours * 60) + fromminutes,
            resultto = (tohours * 60) + tominutes,
            result = resultto - resultfrom,
            hourresult = ~~(result/60),
            minuteresult = ~~(result - hourresult * 60);
        $('#resulthours' + id).val(hourresult);
        $('#resultminutes' + id).val(minuteresult);
    }
});

HMTL HMTL

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input style="margin-left:28px;" type="image" class="add_field_button" value="Add a new row" />
<br />From &rarr;
<input type="text" class="cal" name="fromhours" id="fromhours1" />:
<input type="text" class="cal" name="fromminutes" id="fromminutes1" /> | To &rarr;
<input type="text" class="cal" name="tohours" id="tohours1" />:
<input type="text" class="cal" name="tominutes" id="tominutes1" /> | Result &rarr;
<input type="text" class="cal" name="resulthours" id="resulthours1" />:
<input type="text" class="cal" name="resultminutes" id="resultminutes1" />
<br />
<br />
<div class="input_fields_wrap"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM