gpt4 book ai didi

javascript - 克隆其中包含文本的字段也会克隆文本吗?

转载 作者:行者123 更新时间:2023-11-30 23:44:36 25 4
gpt4 key购买 nike

我有一段代码克隆了三个字段,但是当它克隆这三个字段时,它也克隆了其中输入的文本,有没有办法在克隆时清除字段内部的内容?

$(document).ready(function() {
$('#btnAdd').click(function() {
var num = $('.clonedSection').length;
var newNum = new Number(num + 1);

var newSection = $('#clonedSection' + num).clone().attr('id', 'clonedSection' + newNum);

newSection.children(':first').children(':first').attr('id', 'name' + newNum).attr('name', 'name' + newNum);
newSection.children(':nth-child(2)').children(':first').attr('id', 'age' + newNum).attr('name', 'age' + newNum);
newSection.children(':nth-child(3)').children(':first').attr('id', 'school' + newNum).attr('name', 'school' + newNum);

$('.clonedSection').last().append(newSection);
$('.clonedSection').last().val(ping);

$('#btnDel').attr('disabled','');

if (newNum == 2)
$('#btnAdd').attr('disabled','disabled');
});

$('#btnDel').click(function() {
var num = $('.clonedSection').length; // how many "duplicatable" input fields we currently have
$('#clonedSection' + num).remove(); // remove the last element

// enable the "add" button
$('#btnAdd').attr('disabled','');

// if only one element remains, disable the "remove" button
if (num-1 == 1)
$('#btnDel').attr('disabled','disabled');
});

$('#btnDel').attr('disabled','disabled');
});

提前致谢!

最佳答案

你在哪里 .clone() 您可以找到/清除字段,如下所示:

.clone().find(':text, textarea').val('').end()

如果您没有<textarea>如果你忽略了这一点,它的作用是执行 .find() 要获取文本元素, .val() 清除它们并 .end() 之后将链返回到使用锥形元素,而不是我们正在寻找的文本元素。

或者,如果这些行是您的输入:

newSection.children(....)

只需添加 .val('')在每个的末尾,例如第一个可能如下所示:

newSection.find('> :first > :first').attr({'id': 'name' + newNum, 'name': 'name' + newNum }).val('');

关于javascript - 克隆其中包含文本的字段也会克隆文本吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3467457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com