开发者

Working with dynamically created input arrays in jQuery

开发者 https://www.devze.com 2023-02-15 17:38 出处:网络
I have a dynamic form with the following fields <tr> <td><input name = \"qty[]\" /></td>

I have a dynamic form with the following fields

<tr>
 <td><input name = "qty[]" /></td>
 <td><input name = "color[]" /></td>
 <td><input name = "price[]" /></td>
 <td><input name = "total[]" /></td>
</tr>

I can dynamically add as many rows as I want. What I 开发者_JS百科would like to accomplish is having total = qty*price for each row with a click of a button. Any suggestions?


Modified your HTML a bit

<tr>
 <td><input name = "qty[]" /></td>
 <td><input name = "color[]" /></td>
 <td><input name = "price[]" /></td>
 <td><input name = "total[]" /></td>
 <td><input type='text' name='total' /></td>
</tr>

jQuery code

$("#button1").click(function(){
    var trElems = $("#tab tr");

    trElems.each(function(){
        var qty = $(this).find("input[name='qty[]']").val();
        var price = $(this).find("input[name='price[]']").val();
        var total = parseFloat (qty) * parseFloat (price);

        $(this).find("input[name='total']").val(total);
    });

See a working demo

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号