使用jquery计算表上的select选项值

时间:2016-08-28 21:28:58

标签: javascript jquery html twitter-bootstrap each

我认为我遇到了js和html(bootstrap)的问题, 我从bootsnip获得了这些片段并尝试根据我的工作修改它。 但在第三行,抱歉使用英语拼写错误。

靴子在这里(我的修改):http://bootsnipp.com/snippets/o8r0G

     $(document).ready(function(){
      var i=1;
     $("#add_row").click(function(){
      $('#addr'+i).html("<td>"+ (i+1) +"</td><td><input name='name"+i+"' type='text' placeholder='Name' class='form-control input-md'  /> </td><td><input  name='mail"+i+"' type='text' placeholder='Mail'  class='form-control input-md'></td><td><select class='form-control' name='slct"+i+"' placeholder='Select'><option value='1'>1</option><option value='2'>2</option></select></td>");

      $('#tab_logic').append('<tr id="addr'+(i+1)+'"></tr>');
      i++; 
  });
     $("#delete_row").click(function(){
    	 if(i>1){
		 $("#addr"+(i-1)).html('');
		 i--;
		 }
	 });

});

$('.buttonx').click(function() {
    var total = 0;

    $('#tab_logic tbody tr').each(function(index) { 

        var price = parseInt($(this).find('.optx sltx option value').text()); 
        var quantity = parseInt($(this).find('.optx sltx option').val()); 
        var value = $(this).find('.value');
        var subTotal = price * quantity;

        value.text(subTotal);
        value.text(price);
        total = total + subTotal;
        testotal = price;

    });

    $('.totality').text('Total : '+testotal);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<div class="container">
    <div class="row clearfix">
		<div class="col-md-12 column">
			<table class="table table-bordered table-hover" id="tab_logic">
				<thead>
					<tr >
						<th class="text-center">
							#
						</th>
						<th class="text-center">
							Value1
						</th>
						<th class="text-center">
							Value2
						</th>
						<th class="text-center">
							Select
						</th>
					</tr>
				</thead>
				<tbody>
					<tr id='addr0'>
						<td>
						1
						</td>
						<td>
						<input type="text" name='va10'  placeholder='Val1' class="form-control"/>
						</td>
						<td>
						<input type="text" name='va20' placeholder='Val2' class="form-control"/>
						</td>
						<td class="optx">
						<select class="form-control sltx" name="slct0" placeholder="Select"><option value="1">1</option><option value="2">2</option></select>
						</td>
					</tr>
                    <tr id='addr1'></tr>
				</tbody>
                <tfoot>
                    <tr>
                        <th></th>
                        <th></th>
                        <th><a id="buttonx" class="btn btn-default btn-danger pull-right">Calculate</a></th>
                        <th id="totality" style="vertical-align: middle;">Total : </th>
                    </tr>
                </tfoot>
			</table>
		</div>
	</div>
	<a id="add_row" class="btn btn-default pull-left">Add Row</a><a id='delete_row' class="pull-right btn btn-default">Delete Row</a>
</div>

我正在尝试从表中创建总数,但我无法使用js从表中加载值,也许我在从堆栈上的其他答案得到的脚本上出错了。参考文献如下:getting values from a html table via javascript/jquery and doing a calculation

我想问一下,如何从表格上的选择选项中获得计算,或者从输入文本框中获取计算,而不按下按钮(实时计算)。

先谢谢。

1 个答案:

答案 0 :(得分:0)

嗯,根据我的理解能力,你需要一个实时计算器,它可以在用户输入和离开任何文本框时计算值,或者在不使用计算按钮的情况下更改选择。

在你的代码中有一些错误,我已经构建并更改了你的一些功能来实现实时计算器。计算公式是不同的,因为我无法理解你将如何计算。我正在使用元素的类选择器实现它,并使用它来获取值。

以下是使用点击按钮和实时计算器的更新代码。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<div class="container">
    <div class="row clearfix">
        <div class="col-md-12 column">
            <table class="table table-bordered table-hover" id="tab_logic">
                <thead>
                    <tr >
                        <th class="text-center">
                            #
                        </th>
                        <th class="text-center">
                            Value1
                        </th>
                        <th class="text-center">
                            Value2
                        </th>
                        <th class="text-center">
                            Select
                        </th>
                    </tr>
                </thead>
                <tbody>
                    <tr id='addr0'>
                        <td>
                        1
                        </td>
                        <td>
                        <input type="text" name='va10'  placeholder='Val1' class="form-control va10"/>
                        </td>
                        <td>
                        <input type="text" name='va20' placeholder='Val2' class="form-control va20"/>
                        </td>
                        <td class="optx">
                        <select class="form-control sltx slct0" name="slct0" placeholder="Select"><option value="1">1</option><option value="2">2</option></select>
                        </td>
                    </tr>

                </tbody>
                <tfoot>
                    <tr>
                        <th></th>
                        <th></th>
                        <th><a id="buttonx" class="btn btn-default btn-danger pull-right">Calculate</a></th>
                        <th id="totality" style="vertical-align: middle;">Total : </th>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>
    <a id="add_row" class="btn btn-default pull-left">Add Row</a><a id='delete_row' class="pull-right btn btn-default">Delete Row</a>
</div>

<script type="text/javascript">
     $(document).ready(function(){
      var i=1;
     $("#add_row").click(function(){
      $('#tab_logic').append("<tr id='addr"+i+"'><td>"+ (i+1) +"</td><td><input name='name'"+i+" type='text' class='form-control va10' placeholder='Name' class='form-control input-md'  /> </td><td><input  name='mail'"+i+"' type='text' placeholder='Mail'  class='va20 form-control input-md'></td><td><select class='form-control slct0' name='slct'"+i+" placeholder='Select'><option value='1'>1</option><option value='2'>2</option></select></td></tr");


      i++; 
  });
     $("#delete_row").click(function(){
         if(i>1){
         $("#addr"+(i-1)).html('');
         i--;
         }
     });

});

function calculator(){
var total = 0;

    $('#tab_logic tbody tr').each(function(index) { 

        var val1 = $(this).find('.va10').val().length==0?0:parseInt($(this).find('.va10').val()); 
        var val2 = $(this).find('.va20').val().length==0?0:parseInt($(this).find('.va20').val()); 
        var select = $(this).find('.slct0').val().length==0?0:parseInt($(this).find('.slct0').val());
        var subTotal = (val1 + val2)*select;
        total+=subTotal;    

    });    
    $('#totality').text(total);
}
$('#tab_logic').on('blur change','.va10,.va20,.slct0',function(){
calculator()
})
$('#buttonx').click(function() {
    calculator();
});


</script>