简体   繁体   English

Select 下一个输入类型字段 Jquery

[英]Select next input type field Jquery

<div class="form-row itemrow">
    <div class="col-md-4">
        <label for="item">Item <span style="color:#ef172c;"> *</span></label>
        <input name="item" type="text" data-parsley-trigger="blur" required  class="form-control item" />
    </div>

    <div class="col-md-2">
        <label for="price">Price <span style="color:#ef172c;"> *</span></label>
        <input  name="price" type="number" data-parsley-trigger="blur" required  class="form-control price" />
    </div>

    <div class="col-md-2">
        <label for="qty">Qty <span style="color:#ef172c;"> *</span></label>
        <input  value="1" name="qty" type="number" data-parsley-trigger="blur" required  class="form-control qty" />
    </div>

    <div class="col-md-2">
        <label for="totalforone">Total</label>
        <input name="totalforone" value="100" type="number" data-parsley-trigger="blur"  class="form-control totalforone" />
    </div>

    <div class="col-md-2">
        <button id="addmore" style="height: 34px; margin-top: 2em;" class="btn btn-sm btn-success add-more btn-block" type="button"><i class="glyphicon glyphicon-plus"></i> Add Item</button>
    </div>
</div>

function calculation(){
    var price = $(".price").val();  
    var qty = $(".qty").val();  
    var aa = $(this).next().find('div > .totalforone').val();
    console.log(aa);
};

$(".price").bind('keyup mouseup', calculation);
$(".qty").bind('keyup mouseup', calculation);

I have these rows for multiply price and quantity eah row enter price and qty manually and total in input type total.我有这些行用于乘以价格和数量,每行手动输入价格和数量,并在输入类型总计中总计。 Want to set price x qty = total each row.想要设置价格 x 数量 = 每行总计。 i have an option to add more rows我可以选择添加更多行在此处输入图像描述

You have to pass this to the function so that you can refer that inside the function.您必须将其传递this function,以便您可以在 function 中引用它。 Then you can target the closest row with .itemrow to find the relevant .totalforone to set the calculated value.然后,您可以使用.itemrow定位最近的行以找到相关的.totalforone来设置计算值。 Also, it is better to use input event instead of keyup and mouseup :此外,最好使用input事件而不是keyupmouseup

 function calculation(el){ var price = $(".price").val(); var qty = $(".qty").val(); $(el).closest('.itemrow').find('.totalforone').val(price * qty); }; $('body').on('input', '.price', function(){calculation(this)}); $('body').on('input','.qty', function(){calculation(this)});
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="form-row itemrow"> <div class="col-md-4"> <label for="item">Item <span style="color:#ef172c;"> *</span></label> <input name="item" type="text" data-parsley-trigger="blur" required class="form-control item" /> </div> <div class="col-md-2"> <label for="price">Price <span style="color:#ef172c;"> *</span></label> <input name="price" type="number" data-parsley-trigger="blur" required class="form-control price" /> </div> <div class="col-md-2"> <label for="qty">Qty <span style="color:#ef172c;"> *</span></label> <input value="1" name="qty" type="number" data-parsley-trigger="blur" required class="form-control qty" /> </div> <div class="col-md-2"> <label for="totalforone">Total</label> <input name="totalforone" value="100" type="number" data-parsley-trigger="blur" class="form-control totalforone" /> </div> <div class="col-md-2"> <button id="addmore" style="height: 34px; margin-top: 2em;" class="btn btn-sm btn-success add-more btn-block" type="button"><i class="glyphicon glyphicon-plus"></i> Add Item</button> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM