简体   繁体   中英

How to connect strings and VAR in javascript

This is my script code :

<script type="text/javascript">
<!---Calculation--->
$(document).ready(function(){
    var o = 0;
    o=o+0;
    for(o; o<=100; 0++){
            var price=$("#price").html();
            var i=1;
            var add;
            var subtr;
            var qty;
            $('#add').click(function(){ 
                qty = $('#pro_qty').val();
                add = Number(qty) + Number(i);
                result= Number(price)* (Number(qty) + Number(i));

                $('#pro_qty').val(add);
                $('.result').html(result);
            });

            $('#subtr').click(function(){
                if(qty>1){
                    all_price=$('.result').text();
                    result=Number(all_price)- Number(price);
                    qty=$('#pro_qty').val();
                    subtr = Number(qty)-1; 
                    $('.result').html(result);
                    $('#pro_qty').val(subtr);

                }else{
                    $('#pro_qty').val(1);
                    $('.result').html(price);
                    }
            }); 
    }
});

</script>         

And now I want to connect Var S with the others in FOR loop

For example :

all_price=$('.result'+ s).text(); 

and my foreach code in view.php

<?php 
                    $a=1;
                    foreach ($this->cart->contents() as $cart_item){?>
                    <li>
                        <ul class="Sub-Cart">
                            <li><a href=""><img src="<?php echo base_url()?>upload/<?php echo $cart_item['options']['Image'];?>"/></a></li>
                            <li><a href=""><?php echo $cart_item['name'];?></a> <a href="<?php echo base_url()?>admin_customer/customer_del_cart/<?php echo $cart_item['rowid'];?>"><i class="fa fa-trash-o" title="Xóa"></i></a></li>
                            <input type="hidden" name='' id='rowid' value='<?=$item['rowid']?>'>
                            <li id="price<?php echo $a;?>"><?php echo $cart_item['price'];?></li>
                            <li><input type="submit" name="submit" value="-" id="subtr<?php echo $a;?> " class="subtr" title="Giảm"/>
                                <input type="text" name="text" value="<?php echo $cart_item['qty'];?>" style="width:50px;" id="pro_qty<?php echo $a;?>"/>
                                <input type="submit" name="submit" value="+" id="add<?php echo $a;?>" class="add" title="Tăng"/>
                            </li> 
                            <li  class="result<?php echo $a;?>"><?php echo $cart_item['subtotal'];?></li/>
                        </ul>
                    </li>
                    <?php $a++;}?>

The result I want is like that for example :

  • For loop in javascript : #add1 ,#add2 ,#add3 ....similar to Foreach loop in php : #add1 ,#add2 ,#add3 ....

But I can not run like that =.=

Can anyone help me?

Sorry for my bad English

Thanks in advance

您需要在引号外进行连接:

all_price = $('.result'+s).text();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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