jQuery和动态表行

空气饼干

我正在尝试用jquery来构建一个购物车,并且花了一点时间来弄清楚如何使其全部工作。

我有一个地方可以在我的网站上购买广告空间。将有一个包含5列的表格(邮政编码列(输入),服务列(选择框),广告尺寸(选择框),广告价格(输入只读)和用于添加或删除行的选项列。直到我想为大型或小型广告添加选项并根据广告尺寸收取不同的价格为止。

        <table style='width: 100%;' id='adsTable'>
            <tr class='addedRow'>
                <td>Zip</td>
                <td>Service</td>
                <td>Ad Size</td>
                <td>Price</td>
                <td>Options</td>
            </tr>
            <tr>
                <td><input type='text' maxlength='5' class='zip' /></td>
                <td>
                    <select name='serviceType' class='serviceType rounded'>
                        <option value="">Choose One...</option>
                        <option>Plumbing</option>
                        <option>Roofing</option>
                        <option>HVAC</option>
                        <option>Appliance Repair</option>
                        <option>Flooring</option>
                        <option>Electrical</option>
                        <option>Doors/Windows</option>
                        <option>Siding</option>
                        <option>Other</option>
                    </select>
                </td>
                <td>
                    <select name='' class='ad_size'>
                        <option value='4.99' class='ad_lg'>Large</option>
                        <option value='1.99' class='ad_sm'>Small</option>
                    </select>
                </td>
                <td>
                    <div class="input-prepend">
                        <span class="add-on"><i class="icon-usd"></i></span>
                        <input class='addPrice' value='4.99' type='text' readonly='readonly' />
                    </div>
                </td>
                    <td class='options'>

                    </td>
            </tr>

        </table>

我剥离了jquery并开始了几次尝试以解决所有问题,但是在选择广告尺寸之后,一直坚持如何添加所有输入。这是我来这里之前结束的jQuery

    //Varible that stores the extra table row
    var $adTableRow = "<tr class='addedRow'><td><input type='text' maxlength='5' /></td><td><select name='serviceType' id='serviceType' class='rounded'><option value=''>Choose One...</option><option>Plumbing</option>    <option>Drain Cleaning</option><option>Roofing</option><option>HVAC</option><option>Appliance Repair</option><option>Flooring</option>  <option>Electrical</option><option>Doors/Windows</option><option>Siding</option><option>Other</option></select></td><td><select name='' class='ad_size'><option value='4.99' class='ad_lg'>Large</option><option value='1.99' class='ad_sm'>Small</option></select></td><td><div class='input-prepend'><span class='add-on'><i class='icon-usd'></i></span><input class='addPrice' value='4.99' type='text' readonly='readonly' /></div></td><td class='options'><i class='icon-remove removeAd' title='Remove This Ad'></i></td></tr>";

    $(document).ready(function() {  
        $('#addTotalBox').val("4.99");
        //Assign the right price amount according to users selection
        $(".ad_size").on('change', function() {
            //Grab the value of the select box and store it in a variable
            var ad_cost = $(this).val();
            //Insert the value into the pricing box             
            $(this).parent().next().children().find('.addPrice').val(ad_cost);
            //Set the total box to the right price according to ad size
            $('input').each(function(index,data) {
                var value = $(this).val();
            });
        });

        $('.addRow').on('click', function() {
            $("#adsTable").append($adTableRow);
            firstRow = false;


            //var addedAdPrice = $(this).parent().prev().children().find('.addPrice').val();

        });
                });

谢谢您的帮助

用户名

根据我上面的评论,您需要将逻辑放入函数中,然后在添加新行时重新初始化该函数。

function reInit(){
    $(".ad_size").on('change', function() {
        console.log('test');
        //Grab the value of the select box and store it in a variable
        var ad_cost = $(this).val();
        //Insert the value into the pricing box             
        $(this).parent().next().children().find('.addPrice').val(ad_cost);
        //Set the total box to the right price according to ad size
        $('input').each(function(index,data) {
            var value = $(this).val();
        });
    });
}

然后,调用它:

$('.addRow').on('click', function() {
    $("#adsTable").append($adTableRow);
    firstRow = false;

    reInit();
    //var addedAdPrice = $(this).parent().prev().children().find('.addPrice').val();

});

这是工作中的小提琴http://jsfiddle.net/QwnuR/2/

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

动态行和表

来自分类Dev

jQuery Tablesorter和动态表

来自分类Dev

动态表的行和列

来自分类Dev

使用 XSLT 和 XML 的动态表行

来自分类Dev

jQuery通过动态ID删除表行

来自分类Dev

jQuery动态添加/行到表

来自分类Dev

使用 JQuery/Javascrpit 动态更改表行

来自分类Dev

使用jQuery动态添加和删除行

来自分类Dev

如何获取动态表的列和行的值和大小

来自分类Dev

HTML表中的动态行和复杂行

来自分类Dev

HTML表中的动态行和复杂行

来自分类Dev

使用jQuery动态添加新行和新子行

来自分类Dev

使用jQuery显示和隐藏动态生成的表元素

来自分类Dev

删除表动态的整个列和n列的jquery

来自分类Dev

在动态创建的表上使用CSS和JQuery的最佳实践

来自分类Dev

通过使用angularjs和jquery创建动态可扩展表

来自分类Dev

通过jQuery复制和删除表行

来自分类Dev

jQuery表搜索和行突出显示

来自分类Dev

HTML / jQuery显示和隐藏表行

来自分类Dev

链接表的行和列-jQuery

来自分类Dev

使用行跨度和循环动态生成包含子表的表

来自分类Dev

如何通过jQuery在动态表中获取行索引

来自分类Dev

从动态表中获取一行(jQuery)

来自分类Dev

jQuery添加具有可点击行的动态表

来自分类Dev

在动态生成的表行中创建Jquery datepicker

来自分类Dev

使用jQuery动态将按钮追加到表行

来自分类Dev

如何删除使用JQuery动态表的特定行

来自分类Dev

使用Javascript / JQuery动态填充HTML表中的新行

来自分类Dev

动态创建的表行是否不能由jQuery定位?