更改一个字段的输入值,当另一个字段键入不适用于多个字段
我有一个我有一个表格的网站,该表格有3个字段,可以在单击时多次添加,我的代码如下:
$(document).ready(function() {
var max_fields = 10;
var wrapper = $(".container1");
var add_button = $("#niy");
var x = 1;
$(add_button).click(function(e) {
e.preventDefault();
if (x < max_fields) {
x++;
$(wrapper).append('<div class="col-lg-4 col-sm-6"><div class="mb-4"><label for="client_type">Service Type</label><input type="text" class="form-control" id="client_type" required name="service[]"></div></div><div class="col-lg-4 col-sm-6"><div class="mb-4"><label for="client_type">Price</label><input type="text" class="form-control" id="client_type" required name="price[]"></div></div><div class="col-lg-4 col-sm-6"><div class="mb-4"><label for="client_type">Total</label><input type="text" class="form-control" id="total" required name="total[]"></div></div>'); //add input box
} else {
alert('You Reached the limits')
}
});
$(wrapper).on("click", ".delete", function(e) {
e.preventDefault();
$(this).parent('div').remove();
x--;
})
});
$('input[name="price[]"]').change(function() {
$('input[name="total[]"]').val($(this).val());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row container1">
<div class="col-lg-4 col-sm-6">
<!-- Form -->
<div class="mb-4">
<label for="client_type">Service Type</label>
<input type="text" class="form-control" id="client_type" required name="service[]">
</div>
</div>
<div class="col-lg-4 col-sm-6">
<!-- Form -->
<div class="mb-4">
<label for="client_type">Price</label>
<input type="text" class="form-control" id="client_type" required name="price[]">
</div>
</div>
<div class="col-lg-4 col-sm-6">
<!-- Form -->
<div class="mb-4">
<label for="client_type">Total</label>
<input type="text" class="form-control" id="client_type" required name="total[]">
</div>
</div>
</div>
<a id="niy" class="btn btn-warning" style="color:white">Add New Service Field
<span style="font-size:16px; font-weight:bold;">+ </span>
</a>
我希望总字段从各自的价格字段中显示值
i have a website where i have a form, the form has 3 fields which can be addedd multiple times on click, my code is like below:
$(document).ready(function() {
var max_fields = 10;
var wrapper = $(".container1");
var add_button = $("#niy");
var x = 1;
$(add_button).click(function(e) {
e.preventDefault();
if (x < max_fields) {
x++;
$(wrapper).append('<div class="col-lg-4 col-sm-6"><div class="mb-4"><label for="client_type">Service Type</label><input type="text" class="form-control" id="client_type" required name="service[]"></div></div><div class="col-lg-4 col-sm-6"><div class="mb-4"><label for="client_type">Price</label><input type="text" class="form-control" id="client_type" required name="price[]"></div></div><div class="col-lg-4 col-sm-6"><div class="mb-4"><label for="client_type">Total</label><input type="text" class="form-control" id="total" required name="total[]"></div></div>'); //add input box
} else {
alert('You Reached the limits')
}
});
$(wrapper).on("click", ".delete", function(e) {
e.preventDefault();
$(this).parent('div').remove();
x--;
})
});
$('input[name="price[]"]').change(function() {
$('input[name="total[]"]').val($(this).val());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row container1">
<div class="col-lg-4 col-sm-6">
<!-- Form -->
<div class="mb-4">
<label for="client_type">Service Type</label>
<input type="text" class="form-control" id="client_type" required name="service[]">
</div>
</div>
<div class="col-lg-4 col-sm-6">
<!-- Form -->
<div class="mb-4">
<label for="client_type">Price</label>
<input type="text" class="form-control" id="client_type" required name="price[]">
</div>
</div>
<div class="col-lg-4 col-sm-6">
<!-- Form -->
<div class="mb-4">
<label for="client_type">Total</label>
<input type="text" class="form-control" id="client_type" required name="total[]">
</div>
</div>
</div>
<a id="niy" class="btn btn-warning" style="color:white">Add New Service Field
<span style="font-size:16px; font-weight:bold;">+ </span>
</a>
i want the total field to show the value from its respective price field, however here only first row does that, can anyone please tell me how to fix this, thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
代码中的主要问题是因为您仅绑定
更改
事件,该事件会在页面首次加载时更新字段的值。这忽略了动态添加的所有字段。要修复此使用委托事件处理程序 - 与.delete
按钮一样。话虽如此,值得注意的是,还有其他几个问题需要解决:
id
属性值。他们需要是独一无二的。另外,在内容中根本不使用id
,可以动态重复。使用class
属性。样式
属性使用内联样式。将一种代码放入另一种代码是不好的习惯,例如。您的HTML中的CSS或JS中的HTML。在这种情况下,使用外部样式表。add_button
已经是jQuery对象,您不需要使用$(add_button)
。因此,值得以$
将jQuery对象的变量名称前缀。话虽如此,这是一个工作的演示:
The main issue in your code is because you only bind the
change
event which updates the value of the fields when the page first loads. This ignores all the fields which are added dynamically. To fix this use delegated event handlers - exactly as you are for the.delete
button.That being said, it's worth noting that there's several other issues which need to be addressed:
id
attribute value in the DOM. They need to be unique. Also, don't useid
at all in content which can be repeated dynamically. Useclass
attributes instead.style
attribute. It's bad practice to put one type of code in another, eg. CSS in your HTML, or HTML in your JS. In this case use an external stylesheet.add_button
is already a jQuery object, you don't need to use$(add_button)
. It's worth prefixing variable names that hold jQuery objects with$
for this reason.With all that said, here's a working demo:
尝试以下片段下一次,您会发现运行时输入更新总计。这会促进用户体验。
try below snippet once, you will find runtime oninput updating totals. That imrpoves user experience.