在 jQuery 更改函数上删除和追加元素

发布于 2024-10-25 21:57:49 字数 894 浏览 7 评论 0原文

我们使用以下代码在文本更改上附加更多输入元素:

$(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城°AllureLove 2024-11-01 21:57:49

您可以在回调的开头放置一个 if 语句,以检查 #fields 元素的子元素是否具有类 inputs。如果是,则将其删除。

$(document).ready(function() {
$('#instal').change(function() {
    if($('#fields').children().hasClass('inputs')) {
        $('.inputs').remove();
    }
    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>');
            $(function() {
                 $( ".small" ).datepicker({ minDate: "+" + i.toString() + "M" });
            });
        }
    }  
});

$('.closefield').live('click', function() {
    $(this).parent().remove();
 });

这是用于 jQuery UI 日期选择器:

$(function() {
    $( ".date" ).datepicker();
});

这是用于修复日期范围:

$(function() {
    $( "#datepicker" ).datepicker({ minDate: -20, maxDate: "+1M +10D" });
});

You could put an if statement in the beginning of the callback that checks if the #fields element's children have the class inputs. If it does, then remove them.

$(document).ready(function() {
$('#instal').change(function() {
    if($('#fields').children().hasClass('inputs')) {
        $('.inputs').remove();
    }
    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>');
            $(function() {
                 $( ".small" ).datepicker({ minDate: "+" + i.toString() + "M" });
            });
        }
    }  
});

$('.closefield').live('click', function() {
    $(this).parent().remove();
 });

This is for the jQuery UI datepicker:

$(function() {
    $( ".date" ).datepicker();
});

This is for fixing the range of dates:

$(function() {
    $( "#datepicker" ).datepicker({ minDate: -20, maxDate: "+1M +10D" });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文