在 jQuery 更改函数上删除和追加元素
我们使用以下代码在文本更改上附加更多输入元素:
$(document).ready(function() {
$('#instal').change(function() {
var instalval = parseInt($(this).val());
if(instalval != '') {
for( i = 0; i < instalval; i++) {
$('#fields').append(' <li class="inputs clearfix"><input type="text" class="small" size="30" name="date" placeholder="Date" /> <input type="text" class="small" size="30" name="amount" placeholder="Amount" /> <span title="Remove this" class="ui-icon ui-icon-closethick closefield"></span> </li>');
}
}
});
$('.closefield').live('click', function() {
$(this).parent().remove();
});
});
我们使用 html5 input type="number" 作为值,但是如果我们选择 6,它会添加 6 个元素,之后如果我们选择 12,它会在最后添加的 6 的基础上添加 12元素,因此总计元素将是 18 而不是 12(选择 12 时),那么我们如何修复代码来实现此目的,以及如果我们在选择 12 后再次选择 6 会怎样,我希望这是有意义的。谢谢。
即使你可以帮助我们如何使用 php 将其发布到 mysql ?
We are using following code to append more input element on text change:
$(document).ready(function() {
$('#instal').change(function() {
var instalval = parseInt($(this).val());
if(instalval != '') {
for( i = 0; i < instalval; i++) {
$('#fields').append(' <li class="inputs clearfix"><input type="text" class="small" size="30" name="date" placeholder="Date" /> <input type="text" class="small" size="30" name="amount" placeholder="Amount" /> <span title="Remove this" class="ui-icon ui-icon-closethick closefield"></span> </li>');
}
}
});
$('.closefield').live('click', function() {
$(this).parent().remove();
});
});
We are using html5 input type="number" for value but so it adds 6 elements if we selects 6 and after that if we selects 12 it adds 12 to last added 6 elements so totals elements would be 18 not 12 (on selection of 12), so how can we fix our code to achieve this, and what if we select 6 again after selecting 12, i hope this makes sense. thanks.
Even if you could help how can we post it to mysql using php?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在回调的开头放置一个 if 语句,以检查
#fields
元素的子元素是否具有类inputs
。如果是,则将其删除。这是用于 jQuery UI 日期选择器:
这是用于修复日期范围:
You could put an if statement in the beginning of the callback that checks if the
#fields
element's children have the classinputs
. If it does, then remove them.This is for the jQuery UI datepicker:
This is for fixing the range of dates: