[英]Set value of HTML element from javascript
I am using session variable to keep track of items in the cart. 我正在使用会话变量来跟踪购物车中的物品。 I generate a table using loop to display contents of the cart. 我使用循环生成一个表来显示购物车的内容。 The code for table is as follow: 该表的代码如下:
<?php
$count=0;
$grandTotal=0;
foreach($_SESSION['cart'] AS $product) {
$table=$product['table'];
$id=$product['id'];
$Name=$product['name'];
$qty=$product['quantity'];
$price=$product['price'];
$total=$qty*$price;
$grandTotal +=$total;
?>
<tr>
<td class="cart_product">
<img src=<?php $i=2; echo "images/".$table."/".$id.".jpg"?> height="150" width="150">
</td>
<td class="cart_description">
<h4><a href=""><?php echo $Name?></a></h4>
<p><?php echo "Web ID: ".$id?></p>
</td>
<td class="cart_price">
<p><?php echo $price?></p>
</td>
<td class="cart_quantity">
<div class="cart_quantity_button">
<a class="cart_quantity_up" href="addToCart.php?table=<?php echo $table ?>&action=inc&id=<?php echo $id ?>" id="increment"> + </a>
<input class="cart_quantity_input" type="text" name="quantity" id="qty" value="<?php echo $qty?>" autocomplete="off" size="2">
<!-- <p class="cart_quantity_input" name="qunatity" id="qty"><?php echo $qty?></p>-->
<a class="cart_quantity_down" href="addToCart.php?table=men&action=dec&id=<?php echo $id ?>" name="decreament" > - </a>
</div>
</td>
<td class="cart_total">
<p class="cart_total_price"><?php echo $total ?></p>
</td>
<td class="cart_delete">
<a class="cart_quantity_delete" href="addToCart.php?table=men&action=del&id=<?php echo $id ?>"><i class="fa fa-times"></i></a>
</td>
</tr>
<?php
}
$tax=0.15*$grandTotal;
?>
I am having problem with +
and -
buttons within a
tags. 我有问题, +
和-
内按钮a
标签。 I want to increment or decrement value in input field named quantity
. 我想在输入字段quantity
增加或减少值。 But since i am generating table in a loop i do not know the id of each field. 但是由于我是在循环中生成表,所以我不知道每个字段的ID。 I want to do something like this: 我想做这样的事情:
<script>
$(document).ready(function () {
$("#increment").click(function () {
var oldval=document.getElementById("qty").value;
var newval=parseInt(oldval);
document.getElementById("qty").value=newval++;
});
});
</script>
But this method always increments first quantity field in the table. 但是此方法总是增加表中的第一数量字段。 How do i get ids of other quantity fields in the table? 如何获取表格中其他数量字段的ID?
Dont use ids inside loop.Just use class for that>check the snippet for a smaple demo 不要在循环中使用id。只需使用class>检查代码片段以获取演示模板
$(document).ready(function () { $(".increment").click(function () { var oldval = $(this).prev('.qty').val(); var newval = parseInt(oldval)+ 1; $(this).prev('.qty').val(newval); }); $(".decrement").click(function () { var oldval = $(this).next('.qty').val(); var newval = parseInt(oldval) - 1; $(this).next('.qty').val(newval); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td>sl No</td> <td>name</td> <td>Dept</td> <td>dummy</td> <td>dummy</td> <td>dummy</td> </tr> <tr> <td>1</td> <td>name</td> <td> </td> <td>name</td> <td>name</td> <td> </td> </tr> <tr> <td>2</td> <td>name</td> <td>Dept</td> <td>name</td> <td> <button class="decrement"> - </button> <input type="text" class="qty" value="1"/> <button class="increment">+ </button> </td> <td>name</td> </tr> <tr> <td>3</td> <td>name</td> <td>name</td> <td>name</td> <td>name</td> <td>name</td> </tr> </table>
Don't use same id for multiple elements instead use class like below 不要对多个元素使用相同的id,而应使用如下所示的类
<a class="cart_quantity_up" href="addToCart.php?table=<?php echo $table ?>&action=inc&id=<?php echo $id ?>" class="increment">
same is case of decrement button user class 递减按钮用户类的情况相同
<a class="cart_quantity_down" href="addToCart.php?table=men&action=dec&id=<?php echo $id ?>" name="decreament" class="decreament"> - </a>
You can bind click event to these anchors and change the quantity in input inside same parent div 您可以将click事件绑定到这些锚点并更改同一父div内输入的数量
$(function(){
$('.increment').on('click', function(){
var $parent = $(this).closest('.cart_quantity_button');
var $input = $parent.find('.cart_quantity_input');
var value = $input.val();
value = parseInt(value)+1;
$input.val(value);
});
$('.decreament').on('click', function(){
var $parent = $(this).closest('.cart_quantity_button');
var $input = $parent.find('.cart_quantity_input');
var value = $input.val();
value = parseInt(value)-1;
$input.val(value);
});
});
There are 2 solutions to this problem: 有两种解决方案:
When generating table using php, assign unique id to each row's <a>
and <input>
, such as #increment_id8878
and #input_id8878
. 使用php生成表时,请为每行的<a>
和<input>
分配唯一的ID,例如#increment_id8878
和#input_id8878
。 Then when #increment
or #decreament
button is clicked, operate <input>
with corresponding id. 然后,当单击#increment
或#decreament
按钮时,用相应的ID操作<input>
。
Operate <input>
element with jQuery's relative selector. 使用jQuery的相对选择器操作<input>
元素。
For example: 例如:
$('.increment').click(function() {
var inputEle = $(this).siblings('input');
var originVal = inputEle.val();
inputEle.val(parseInt(originVal) + 1);
});
A jsfiddle is made for the 2nd solution. jsfiddle用于第二种解决方案。
BTW, duplicate id should be avoided in HTML code. 顺便说一句,在HTML代码中应避免重复的ID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.