简体   繁体   English

如何添加具有相同类名的表单的输入字段

[英]How to add input field of a form with same class name

I have a JavaScript problem which i still can find a way out with. 我有一个JavaScript问题,但仍然可以找到解决方法。

I have a table which is been formed dynamically and it contains form fields. 我有一个动态形成的表格,其中包含表格字段。

code: 码:

      var table = document.getElementById("table");
      var row = table.insertRow(-1);
      var cell1 = row.insertCell(-1); 
      var cell2 = row.insertCell(-1);
      var cell3 = row.insertCell(-1);
      var cell4 = row.insertCell(-1);
      var cell5 = row.insertCell(-1);
      cell1.innerHTML = count; //this is index, set above as var count = 1;

      cell2.innerHTML = name; //this is a variale dynamically generated
      cell3.innerHTML = '<input type="text" name="quantity[]" onchange="addAll()" class="quantity" id="quantity" value="1" data-price="'+price+'" />'
      cell4.innerHTML = price;
      cell5.innerHTML = '<a href="#" onclick="deleteRow('+count+')">Delete</a>';

he above will come up with something like: 他上面将提出类似以下内容:

   <input type="text" name="quantity[]" onchange="addAll()" class="quantity" value="1" data-price="4" />
   <input type="text" name="quantity[]" onchange="addAll()" class="quantity" value="34" data-price="5" />
   <input type="text" name="quantity[]" onchange="addAll()" class="quantity" value="12" data-price="8" />

I then have another input field with lable TOTAL 然后我有另一个输入标签,标签总数为

  <input name="total" id="total" />

I want to always loop through the input form above after every onchange() and do a calculation. 我想始终在每次onchange()之后遍历上面的输入表单并进行计算。

 var total = class with quantity[0] * data-price + class with quantity[1] * data-price //and so on for all the input (multiply the input value by the data-price attribute and add all together).

so i can put it in the function: 所以我可以把它放在函数中:

 function addAll() {
    //code that returns the total
    document.GetElementById('total').value = total;
 }

Im looking at jQuery $("className").each(function () but really confuse 我正在看jQuery $(“ className”)。each(function(),但确实让我感到困惑

If you want to visit each of a class, the following jquery would do it: 如果您想访问每个类,则以下jquery可以做到:

var total=0;
$(".quantity").each(function() {
    total=total+$(this).val();
});

The "." “。” means "select the class", like with css. 表示“选择类别”,就像使用CSS一样。 You can't do the same with id's because id's are meant to be unique, so jquery will return only the first element. 您无法对id进行相同的操作,因为id的含义是唯一的,因此jquery将仅返回第一个元素。

You can use below code to loop through your fields: 您可以使用以下代码遍历您的字段:
$('. quantity'). $('。数量')。 each(function(){ //your calculation }) ; each(function(){//您的计算});

function addAll()
{
   var total = 0;
    $('.quantity).each(function()
    {
         if($(this).attr('name')!='Total')
         {
             var val = $(this).attr('value')
             var dat = $(this).attr('data-price')
             total = total + (val*dat)
         }
     });
     document.GetElementById('total').value = total
 }

I believe this will do what you're asking for... Although it is not attached to .on('change') event. 我相信这会满足您的要求...尽管它没有附加到.on('change')事件上。 You may want to change that part a bit yourself. 您可能需要自己更改一下。 Good luck learning jQuery. 祝您学习jQuery好运。

jQuery .each() is used! 使用jQuery .each()

var value = 0, price = 0;

$('.quantity').each(function () {
    var val = value += Number($(this).val()),
        data = price += $(this).data('price'),
        total = val * data;

    $('#total').val(total);
});

JSFiddle Example - You may want to take a look... JSFiddle示例 -您可能想看看...

I think this is perfect code for your requirement 我认为这是满足您要求的完美代码

$('.quantity').change(function(){
     var total = 0;
     $('.quantity').each(function(index,element){
    total += ( $(element).val())*($(element).prop('data-price'));

     });

     $('#total').val(total);
  });

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

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