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

时间:2013-11-26 12:51:12

标签: javascript php jquery html

我有一个表单,我将用于编辑数据库中的数据。它有3个由Category,Subcategory和item组成。它们是根据前一个下拉列表中选择的值动态填充的。在选择了所有3个下拉列表后,所选项目的当前数据将通过jQuery添加到表单中。加载方法。

虽然我目前能够毫无问题地显示现有数据,但添加到表单的数据不会响应我的jQuery代码。我怀疑这是因为jQuery在$(document).ready中,因此在添加项目之前运行。

如果是这样,我该怎样做才能使我的jQuery动态添加部分功能?

我的jQuery代码:

// 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);
    });

   }
});

动态添加内容:

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 个答案:

答案 0 :(得分:0)

查看http://api.jquery.com/on/

您需要将jquery代码更改为:

$(document).on('click','#BizEditItemRadioYes' ,function(){

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

});  

将文档替换为非动态的容器。