【问题标题】:jquery on() function and get value/text using traversingjquery on() 函数并使用遍历获取值/文本
【发布时间】:2015-06-24 19:49:42
【问题描述】:

如何使用on() 函数获取值/文本?

来自 ajax 响应的带有 php 的 html

echo "<div class='cartItem-buyingItem'>";
        echo "<a href='#' class='buyingItem-closed styleCrl-gry1 stxt10'>X<span class='pid'>".$row["prdct_id"]."</span><br></a>";           
        echo "<div class='buyingItem-img'>";
            echo "<img src='../".$row['prdct_picture']."'>";
        echo "</div>";
        echo "<div class='buyingItem-details styleCrl-gry'>";
            echo "<div class='buyingItem-desc styleCrl-gry2 stxt12'>";
                echo substr($row['prdct_description'],0,130)."...";
            echo "</div>";
        echo "</div>";
        echo "<div class='buyingItem-origPrcng'>";
            echo "<div class='buyingItem-unitPrice byngItm-untPrc stxt16 styleCrl-green'>". number_format($row['prdct_newPrice'] ,2) ."</div>";
            echo "<div class='buyingItem-disPrice stxt12 styleCrl-gry2'>". number_format($row['prdct_price'] ,2) ."</div>";
        echo "</div>";
        echo "<div class='buyingItem-qty'>";
            echo "<select class='buyingItem-qty-total'>";
                echo "<option value='".array_count_values($allcart_num)[$row['prdct_id']]."'>".array_count_values($allcart_num)[$row['prdct_id']]."</option>";
                echo "<option value='4'>4</option>";
                echo "<option value='6'>6</option>";
                echo "<option value='8'>8</option>";                
            echo "</select>";
        echo "</div>";
        echo "<div class='buyingItem-subPrcng'>";
            echo "<div class='buyingItem-ttlPrice stxt16 styleCrl-green'>". number_format( bcmul(array_count_values($allcart_num)[$row['prdct_id']] , $row['prdct_newPrice'] ,2) ,2) ."</div>";
            echo "<div class='buyingItem-ttlDisPrice stxt12 styleCrl-gry2'>". number_format( bcmul(array_count_values($allcart_num)[$row['prdct_id']] , $row['prdct_price'] ,2) ,2) ."</div>";
        echo "</div>";
    echo "</div>";  

脚本

$(".cartItem-Load").on('change', '.buyingItem-qty select', function(){      
    alert($(this).val());
    alert($(this).find('.buyingItem-unitPrice').text());//always null/undefined result 
})

如何在 jquery 上使用右遍历获取其他父级子级值?

我需要得到选择.buyingItem-unitPrice的值并乘以选择值.buyingItem-qty select

【问题讨论】:

    标签: javascript jquery ajax jquery-traversing


    【解决方案1】:

    您的代码的问题是find() 用于查找子元素,而.buyingItem-unitPrice 元素是select 的父元素的兄弟元素的子元素。要检索它,您可以使用closest() 的组合来获取公共父级,然后使用find()。试试这个:

    $(".cartItem-Load").on('change', '.buyingItem-qty select', function() { 
        var unitPrice = $(this).closest('.cartItem-buyingItem').find('.buyingItem-unitPrice').text();
        var total = parseInt($(this).val(), 10) * parseFloat(unitPrice);
        console.log(total);
    })
    

    【讨论】:

    • 获取 unitPrice 的值/文本但相乘后返回 NaN 的工作
    • 为你更新了答案。
    • 现在它可以工作了 :) 我添加了 parseFloat(unitPrice.replace(',', '') 逗号。但是我怎样才能返回 2 位小数的值?//123.20
    • :) 更新:var total = (parseInt($(this).val(), 10) * parseFloat(unitPrice.replace(',', ''))).toFixed( 2);
    • 先生,我怎样才能用新的总数更新 .buyingItem-ttlPrice? $(this).closest('.cartItem-buyingItem').find('.buyingItem-ttlPrice').innerHTML = total;
    猜你喜欢
    • 2013-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-13
    • 2016-12-26
    • 2020-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多