doulu4534 2013-11-26 12:51
浏览 47
已采纳

将jQuery应用于多个不同的动态创建的HTML元素

I have a form i will be using to edit data in my database. It has 3 which consists of Category,Subcategory and item.The are dynamically populated based on the value selected in the previous dropdown.After all 3 dropdowns are selected, the current data on the selected item will then be added into the form via jQuery's .load method.

While i currently am able to display the existing data without a problem, the data which is added to the form do not respond to my jQuery code. I suspect this is because the jQuery is inside $(document).ready and as such, runs before the items are added.

If so, what can i do to make the dynamically added parts function with my jQuery?

My jQuery code:

// Shows the details when yes is clicked
$('#BizEditItemRadioYes').click(function(){

    $('.BizEdititemDiscountDetails').show();

});
// Hides the details when no is clicked
$('#BizEditItemRadioNo').click(function(){

    $('.BizEdititemDiscountDetails').hide();

});
// calculates the discounted price when discount is checked yes
$('#BizEditItemRadioYes').change(function(){
    if($(this).val() == 'Yes'){

    // calculates discount price when discount percentage changes
    $('#BizEditItemPercent').change(function(){
        var percent=$('#BizEditItemPercent').val();
        var price=$('#BizEditItemPrice').val();
        var discountpercent=percent / 100;
        var discountprice=price * discountpercent;
        $('#BizEditItemDiscountPrice').val(price - discountprice);
    });
    // calculates discount price when discount price changes
    $('#BizEditItemPrice').change(function(){
        var percent=$('#BizEditItemPercent').val();
        var price=$('#BizEditItemPrice').val();
        var discountpercent=percent / 100;
        var discountprice=price * discountpercent;
        $('#BizEditItemDiscountPrice').val(price - discountprice);
    });

   }
});

Dynamically added content:

echo"<div class='BizEditItemDetails'>";
            echo"<label for='BizEditItemName'>Name:</label>";
            echo"<input class='BizEditItemInput' type='text' id='BizEditItemName' name='BizEditItemName' size='30' maxlength='50' value=\"$name\">";
            echo"<span></span>";
        echo"</div>";

        echo"<div class='BizEditItemDetails'>";
            echo"<label for='BizEditItemPrice'>Price:</label>";
            echo"<input class='BizEditItemInput' type='number' id='BizEditItemPrice' name='BizEditItemPrice' step='any' min=0 value=\"$price\">";
            echo"<span></span>";
        echo"</div>";

        echo"<div class='BizEditItemDetails'>";
            echo"<label>Discount:</label>";
                echo"<input type='radio' class='BizEditItemInput' value='Yes' id='BizEditItemRadioYes' name='BizEditItemDiscount'>Yes";
                echo"<input type='radio' checked='checked' class='BizEditItemInput' value='No'  id='BizEditItemRadioNo' name='BizEditItemDiscount' step='any'>No";
            echo"</span></span>";
        echo"</div>";


        echo"<div class='BizEditItemDetails'>";
            echo"<div class='BizEdititemDiscountDetails'>";
                echo"<label for='BizEditItemPercent'>Discount Percentage</label>";
                echo"<input class='BizEditItemInput' type='number' id='BizEditItemPercent' name='BizEditItemPercent' step='any' min=0 value=\"$discountpercent\">%";
                echo"<span></span>";
            echo"</div>";
        echo"</div>";

        echo"<div class='BizEditItemDetails'>";
            echo"<div class='BizEdititemDiscountDetails'>";
                echo"<label for='BizEditItemDiscountPrice'>Discounted Price:</label>";
                echo"<input class='BizEditItemInput' type='number' id='BizEditItemDiscountPrice' name='BizEditItemDiscountPrice' step='any' min=0 value=\"$discountprice\" readonly >";
                echo"<span></span>";
            echo"</div>";
        echo"</div>";

        echo"<div class='BizEditItemDetails'>";
                echo"<label for='BizEditItemQty'>Quantity:</label>";
                echo"<input class='BizEditItemInput' type='number' id='BizEditItemQty' name='BizEditItemQty' min=0 value=\"$qty\">";
                echo"<span></span>";
        echo"</div>";



        echo"<div class='BizEditItemDetails'>";
            echo"<label for='BizEditItemDesc'>Desc:</label>";
            echo"<textarea class='BizEditItemInput' id='BizEditItemDesc' name='BizEditItemDesc' cols='30' rows='5'>$desc</textarea>";
            echo"<span></span>";
        echo"</div>";

        echo"<div class='BizEditItemDetails'>";
            echo"<input class='BizEditItemInput' style='margin-left:360px' type='submit' name='BizEditItemSubmit' value='Submit'>";
        echo"</div>";
  • 写回答

1条回答 默认 最新

  • duanjuan3931 2013-11-26 12:54
    关注

    Take a look at http://api.jquery.com/on/

    You need to change your jquery code to:

    $(document).on('click','#BizEditItemRadioYes' ,function(){
    
        $('.BizEdititemDiscountDetails').show();
    
    });  
    

    Replace Document with a container which is not dynamic.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 poi合并多个word成一个新word,原word中横版没了.
  • ¥15 【火车头采集器】搜狐娱乐这种列表页网址,怎么采集?
  • ¥15 求MCSCANX 帮助
  • ¥15 机器学习训练相关模型
  • ¥15 Todesk 远程写代码 anaconda jupyter python3
  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?