简体   繁体   English

Jquery 帮助选择输入字段

[英]Jquery help selecting input field

I am trying to select the input field that is added when pressing the "add info" button.我正在尝试 select 按下“添加信息”按钮时添加的输入字段。

Now the first input field is removed when pressed "Remove", it should be the input field that was added: http://jsfiddle.net/gDChA/14/ Jquery:现在第一个输入字段在按下“删除”时被删除,它应该是添加的输入字段: http://jsfiddle.net/gDChA/14/ Jquery:

function findLastInput ( element ) {
  return $( element ).parent().prev().find('input').last();
}
$('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);
})
$('button.remove').click ( function(e) {
    $(this).parent().find('button.add').show();
    $(this).hide();
    findLastInput(this).remove('input');
});

It is this selector that is wrong: findLastInput(this).remove('input');错误的是这个选择器: findLastInput(this).remove('input');

HTML: HTML:

<div class="input string optional"><label for="virksomhed_navn" class="string optional"> Navn</label><input type="text" size="50" name="virksomhed[navn]" maxlength="255" id="virksomhed_navn" class="string optional"></div>
<div class="button-row" style="font-size: 11px; width: 110px; float: right; margin-top: -10px; margin-right: 16px;">
      <button class="add" style="font-size: 11px;">Add info</button>
      <button class="remove" style="font-size: 11px;">Remove</button>
    </div>

    <div class="input string optional"><label for="virksomhed_name" class="string optional">Name</label><input type="text" size="50" name="virksomhed[name]" maxlength="255" id="virksomhed_name" class="string optional"></div>
<div class="button-row" style="font-size: 11px; width: 110px; float: right; margin-top: -10px; margin-right: 16px;">
      <button class="add" style="font-size: 11px;">Add info</button>
      <button class="remove" style="font-size: 11px;">Remove</button>
    </div>


    <div class="input string optional"><label for="virksomhed_pis" class="string optional">Pis</label><input type="text" size="50" name="virksomhed[v]" maxlength="255" id="virksomhed_pis" class="string optional"></div>
<div class="button-row" style="font-size: 11px; width: 110px; float: right; margin-top: -10px; margin-right: 16px;">
      <button class="add" style="font-size: 11px;">Add info</button>
      <button class="remove" style="font-size: 11px;">Remove</button>
    </div>

This line, $(this).closest('.button-row').append(element); 这一行, $(this).closest('.button-row').append(element); , adds the new input field to div.button-row instead of div.input . ,将新的输入字段添加到 div.button-row而不是 div.input So when you delete the last input in div.input , the first and only input field get deleted, leaving the new one in div.button-row . 因此,当您删除 div.input中的最后一个输入时,第一个也是唯一一个输入字段将被删除,而将新输入字段留在 div.button-row中。

EDIT: Just noticed that you say you want the input in another comment.编辑:刚刚注意到您说您希望在另一条评论中输入。 In that case you need to update the part that removes the input.在这种情况下,您需要更新删除输入的部分。

To select an input field, use the focus method.到 select 一个输入字段,使用焦点方法。 Keep in mind that the field must be in the document, visible, and enabled (ie that it can be selected)请记住,该字段必须在文档中、可见且已启用(即可以选择)

So you only need to change the last line of each function:所以只需要更改每个function的最后一行:

$('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();
});

Here you go给你 go

Working demo工作演示

Repleace $(this).closest('.button-row').append(element);替换$(this).closest('.button-row').append(element); in your code by $(this).parent().append(element);在你的代码中$(this).parent().append(element); and findLastInput(this).remove('input');findLastInput(this).remove('input'); by $(this).parent().find("input").remove();通过$(this).parent().find("input").remove(); . .

Instead of:代替:

findLastInput(this).remove('input');

you need:你需要:

$(this).siblings('input').remove();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM