Jquery循环数组选择器与自定义元素ID的

Jquery looped array selectors with custom element ID's

本文关键字:元素 ID 自定义 循环 数组 选择器 Jquery      更新时间:2023-09-26

浏览其他帖子时,我必须要么我的代码有问题,要么我的逻辑从根本上存在缺陷。

所以发生的事情是我有一系列被调用/写入的数组元素,这些数组元素需要由 java 脚本修改子元素。

在我需要添加一个数组之前,一切都很正常,即我对下面的函数使用单个元素选择器 ID 并得到了正确的结果。但是,在循环中添加唯一 ID 后,它不想更改。

所以这就是发生的事情,我有一个隐藏的单独div。这将打印出数组中有多少元素,因为它是一个 PHP 会话变量。$Carray是一个计数函数,可以正常工作。

<div class="Carray"><?php echo $Carray;?></div>

然后,当项目循环时,它们会添加一个数组 ID

<?php 
$arrayID = -1;
foreach($_SESSION['activity'] as $key){ 
foreach($key as $list){ 
$arrayID += 1;      
?>    
    <div id="subP_<?php echo $arrayID;?>" class="booking_action">-</div>
    <div id="booking_people_<?php echo $arrayID;?>" class="booking_people_number">
        <?php echo $list["i_quantity"] ?>
    </div>
    <div id="addP_<?php echo $arrayID;?>" class="booking_action">+</div>
<?php }} ?>

然后在 Javascript 中,我调用一个循环函数,该函数通过有多少个$Carray元素进行计数,然后将正确的函数操作对应于正确的div ID

   //get the counted array variable and force as an int
    var js_var = parseInt($('.Carray').html(),10);
    // loop for however many array elements there are
    for (i = 0; i < js_var; i++){
    // get the amount of people from a field 
    var ppl_P =  parseInt($('#booking_people_'+i).html(),10);
    // subtract 1 person and then change the output to match the new people count 
    $("#subP_"+i).click(function() {
      if(ppl_P >= 2){
        ppl_P -= 1;
        $('#booking_people_'+i]).html(ppl_P);
      }
    });
    // Add 1 person and then change the output to match the new people count 
    $("#addP_"+i).click(function() {
        ppl_P += 1;
        $('#booking_people_'+i).html(ppl_P);
    });
    }

******************************编辑**********************

因此,基于Jimmi Elofsson的答案,我想将其扩展为效果不在父/子选择器中的元素。选择器是".booking_price_inner",这是存储在其他地方的另一个div。我假设需要正确语法的行是标记的行。

".booking_base_price"位于父/子元素内。

    $(".subPerson").click(function() {
    // Subtract Person 
    var subPeopleCount = getCurrentCountByPeopleItem($(this).parent()) - 1;
    $(this).parent().children('.booking_people_number').html(subPeopleCount>1 ? subPeopleCount : 1);
    //Change price
    var totalPriceS = subPeopleCount * getBasePrice($(this).parent());
    $(this).parent().children('.booking_price_inner').html(totalPriceS); <-------
});
$(".addPerson").click(function() {
    //Add person
    var addPeopeCount = getCurrentCountByPeopleItem($(this).parent()) + 1;
    $(this).parent().children('.booking_people_number').html(addPeopeCount);
    //Change price
    var totalPriceA = addPeopleCount * getBasePrice($(this).parent());
    $(this).parent().children('.booking_price_inner').html(totalPriceA); <------
});
// get the number of people in the specific array
function getCurrentCountByPeopleItem(peopleItem) {
    return parseInt(peopleItem.children('.booking_people_number').html());
}
//get the base price
function getBasePrice(peoplePrice){
     return parseInt(peoplePrice.children('.booking_base_price').html());
}

标记

<div id="<?php echo $arrayID;?>" class="booking_people">
    <div class="booking_date_header">People:</div>
    <div class="subPerson booking_action">-</div>
    <div class="booking_people_number"><?php echo $list["i_quantity"] ?></div>
    <div class="addPerson booking_action">+</div>
    <div class="booking_base_price"><?php echo $list["i_base_price"] ?></div>
</div>
<div class=spacer></div>
<div class=cost>
    <div class=booking_price_inner></div>
</div>

如果你不介意,我在你的代码中做了一些更改。

您可以在一些 DOM 遍历的帮助下使您的 Add/Substract 元素使用相同的单击函数。这样你就不需要CArray来跟踪按钮了。

Javascript:

// subtract 1 person and then change the output to match the new people count  
$(".subPerson").click(function() {
    var subPeopleCount = getCurrentCountByPeopleItem($(this).parent()) - 1;// Substract by one.
    $(this).parent().children('.booking_people_number').html(subPeopleCount>1 ? subPeopleCount : 1);
});
// Add 1 person and then change the output to match the new people count 
$(".addPerson").click(function() {
    var addPeopeCount = getCurrentCountByPeopleItem($(this).parent()) + 1; // Increase by one.
    $(this).parent().children('.booking_people_number').html(addPeopeCount);
});
function getCurrentCountByPeopleItem(peopleItem) {
    return parseInt(peopleItem.children('.booking_people_number').html());
}

.PHP:

<?php 
$arrayID = -1;
foreach($_SESSION['activity'] as $key){ 
foreach($key as $list){ 
$arrayID += 1;      
?>
<div class="booking_people_item" id="<?php echo $arrayID;?>">
    <div class="subPerson booking_action">-</div>
    <div class="booking_people_number">
        <?php echo $list["i_quantity"] ?>
    </div>
    <div class="addPerson booking_action">+</div>
</div>
<?php }} ?>

我在你的元素周围添加了一个名为 booking_people_item 的div 包装器。

从我所看到的 - 你是循环中的明确点击函数。这些单击函数具有对ppl_P变量的引用。当您定义点击时,它似乎没问题。但是,当触发单击时,ppl_P变量已设置为上次迭代循环中的值。所以,每当你调用那个点击函数时,它总是有相同的结果,不是吗?

执行此操作的正确方法是将此ppl_P变量作为参数传递,这样您就不会引用已在另一个作用域中更改的变量。像这样:

function addClickFunction(i, ppl_P){
$("#subP_"+i).click(function() {
  if(ppl_P >= 2){
    ppl_P -= 1;
    $('#booking_people_'+i]).html(ppl_P);
  }
});
// Add 1 person and then change the output to match the new people count 
$("#addP_"+i).click(function() {
    ppl_P += 1;
    $('#booking_people_'+i).html(ppl_P);
});
}

然后在循环中使用此函数:

var ppl;
for (i = 0; i < js_var; i++){
    ppl =  parseInt($('#booking_people_'+i).html(),10);
    addClickFunction(i, ppl);
}

这还没有经过测试,但我很确定你会明白这一点:)