[英]Multiply two textbox inside loop and display result on third textbox
This code will result for 10 lines of textbox1, textbox2, and textbox3. 此代码将产生10行textbox1,textbox2和textbox3。 If I enter value in first line of textbox which is in loop1. 如果我在loop1中的文本框的第一行中输入值。 for example 1 and 3 it will multiply and result is 3 . 例如1和3,它将相乘,结果为3。 but then it will display the result in all loop. 但随后它将在所有循环中显示结果。 How can i make it only in loop1.? 我如何只能在loop1中制作它?
Example 例
QTY PRICE AMOUNT
line1 1 2 3
line2 3
line3 3
line4 3
line5 3
line6 3
line7 3
line8 3
line9 3
line10 3
PHP HTML CODE PHP HTML代码
<?php for($x=1; $x<=10; $x++){?>
<tr>
<td><input type="text" name="qty<?=$x;?>" class="qty form-control" size="6"/></td>
<td><input type="text" name="price<?=$x;?>" class="price form-control" /></td>
<td><input type="text" name="amount<?=$x;?>" class="amount form-control"></td>
</tr>
<?php } ?>
JS CODE JS代码
<script>
$(".price,.qty").keyup(function () {
$('.amount').val($('.qty').val() * $('.price').val());
});
</script>
this will be much easy if you write separate for each event try this, 如果您为每个事件分别编写代码,这将非常容易,
<script>
$(document).ready(function(){
$(".qty").keyup(function () {
$(this).closest('tr').find('.amount').val($(this).val() * $(this).closest('tr').find('.price').val());
});
$(".price").keyup(function () {
$(this).closest('tr').find('.amount').val($(this).closest('tr').find('.qty').val() * $(this).val());
});
});
</script>
<script>
$(".price,.qty").keyup(function () {
var curName = $(this).attr('name');
var reqName = '';
if (curName.indexOf("qty") != -1) {
reqName = curName.substr(3);
} else {
reqName = curName.substr(5);
}
$("input[name='amount"+reqName+"']").val($("input[name='qty"+reqName+"']").val() * $("input[name='price"+reqName+"']").val());
});
</script>
I solve it .. I just change it instead of using Class I use ID then put variable x inside the id and also put variable x on my JS 我解决了..我只是更改它而不是使用Class我使用ID,然后将变量x放在id内,然后将变量x放在我的JS上
<?php for($x=1; $x<=10; $x++){?>
<tr>
<td><input type="text" name="qty<?=$x;?>" id="qty<?=$x;?>" class="form-control" size="6"/></td>
<td><input type="text" name="price<?=$x;?>" id="price<?=$x;?>" class="orm-control" /></td>
<td><input type="text" name="amount<?=$x;?>" id="amount<?=$x;?>" class="form-control"></td>
</tr>
<?php } ?>
$("#price<?=$x;?>,#qty<?=$x;?>").keyup(function () {
$('#amount<?=$x;?>').val($('#qty<?=$x;?>').val() * $('#price<?=$x;?>').val());
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.