简体   繁体   English

Jquery 帮助选择输入字段

[英]Jquery help selecting input fields

I am trying to rewrite this Jquery http://jsfiddle.net/Claudius/gDChA/4/ to use this HTML instead http://pastie.org/2370829 I am trying to rewrite this Jquery http://jsfiddle.net/Claudius/gDChA/4/ to use this HTML instead http://pastie.org/2370829

Jquery: Jquery:

$('button.add', '#companyinfo').live('click', function(e) {
    $(this).parent().find('button.remove').show();
    $(this).hide();

    var element = $(this).parents('.input').find('input').last().clone().prop('value','');

    var name = element.prop('name');
    var pattern = new RegExp(/\[(.*?)\]/);
    var info = name.match(pattern)[1];
    var newname = name.replace(pattern, '[' + info + 'info' + ']');
    var newid = element.prop('id') + 'info';  

    element.prop('name', newname);
    element.prop('id', newid);    
    element.insertAfter($(this).parents('.input').find('input').last());

})
$('button.remove', '#companyinfo').live('click', function(e) {
    $(this).parent().find('button.add').show();
    $(this).hide();
    $(this).parents('.input').find('input').last().remove('input');
});

Old HTML老HTML

<fieldset id='companyinfo'><legend>Company info</legend>

    <div class='input string optional'>
        <label for='company_navn' class='string optional'>Count</label>
        <input type='text' size='50' name='company[count]' id='company_navn' maxlength="255"  class='string optional' />
        <div class='button-row'>
            <button class='add'>Add info</button>
            <button class='remove'>Remove</button>
        </div>
    </div>

    <div class="input string optional">
        <label for="company_navn" class="string optional">Navn</label>
        <input type="text" size="50" name="company[navn]" maxlength="255" id="company_navn" class="string optional">
        <div class='button-row'>
            <button class='add'>Add info</button>
            <button class='remove'>Remove</button>
        </div>
    </div>    
</fieldset>

New 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>

How can I select the inputs fields in the new HTML and create the same functionality?我如何 select 新 HTML 中的输入字段并创建相同的功能?

There are two places you need to change.有两个地方需要更改。

  1. The buttons that the handlers hook on to.处理程序挂钩的按钮。
  2. The way you get the last input field.获取最后一个输入字段的方式。

And there are some minor enhancements you can do:您可以做一些小的改进:

  1. Since the buttons are static, you can use click() to add event handler instead of live()由于按钮是 static,您可以使用 click() 来添加事件处理程序而不是 live()
  2. At three place you need to find the last input field relative to current button;在三个地方,您需要找到相对于当前按钮的最后一个输入字段; making it a function would make it easier to understand and modify.使其成为 function 将使其更易于理解和修改。
  3. most jQuery set functions, including prop(), can take an object to set multiple values.大多数 jQuery 设置函数,包括 prop(),都可以使用 object 设置多个值。 This is usually better then calling the same function many times.这通常比多次调用相同的 function 更好。

So, the final result:所以,最终结果:

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

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

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