Javascript Jquery帮助选择输入字段
我试图选择按下“添加信息”按钮时添加的输入字段 现在,当按下“移除”时,第一个输入字段被移除,它应该是添加的输入字段: Jquery: 这个选择器是错误的:Javascript Jquery帮助选择输入字段,javascript,jquery,Javascript,Jquery,我试图选择按下“添加信息”按钮时添加的输入字段 现在,当按下“移除”时,第一个输入字段被移除,它应该是添加的输入字段: Jquery: 这个选择器是错误的:findLastInput(this.remove('input') HTML: Navn 添加信息 去除 名称 添加信息 去除 法律与正义党 添加信息 去除 给你 工作 Repleace$(this).closest('.button行').append(元素)和findLastInput(this).remove('input')
findLastInput(this.remove('input')代码>
HTML:
Navn
添加信息
去除
名称
添加信息
去除
法律与正义党
添加信息
去除
给你
工作
Repleace$(this).closest('.button行').append(元素)通过$(this.parent().append(element)在代码中添加code>
和findLastInput(this).remove('input')通过$(this.parent().find(“input”).remove()代码>这一行,$(这一行)。最近('.button行')。追加(元素)
,将新输入字段添加到div.button-row
而不是div.input
。因此,当您删除div.input
中的最后一个输入时,第一个也是唯一一个输入字段将被删除,新的输入字段将保留在div.button-row
中
编辑:刚刚注意到你说你想在另一条评论中输入。在这种情况下,您需要更新删除输入的零件
要选择输入字段,请使用焦点方法。请记住,该字段必须位于文档中、可见且已启用(即可以选择)
因此,您只需更改每个函数的最后一行:
$('button.add').click ( function(e) {
$(this).parent().find('button.remove').show();
$(this).hide();
var element = findLastInput(this).clone();
var name = element.prop('name');
var pattern = new RegExp(/\[(.*?)\]/);
var info = name.match(pattern)[1];
element.prop({
'value': '',
'name' : name.replace(pattern, '[' + info + 'info' + ']'),
'id' : element.prop('id') + 'info',
'type' : 'input'
});
$(this).closest('.button-row').append(element);
element.focus();
})
$('button.remove').click ( function(e) {
$(this).parent().find('button.add').show();
$(this).hide();
$(this).closest('.button-row').find('input').remove();
});
而不是:
findLastInput(this).remove('input');
你需要:
$(this).siblings('input').remove();
包括一个提琴是好的,另外还要把你的代码放在你的问题中。添加的输入字段应该在带有类按钮行的div中。我希望添加的输入字段被添加到div.button-row中
findLastInput(this).remove('input');
$(this).siblings('input').remove();