简体   繁体   English

Jquery 帮助 append HTML 不工作

[英]Jquery help append HTML not working

I am trying to add this HTML also when a input field is added:当添加输入字段时,我也在尝试添加此 HTML:

<div id="redDiv" style="width:30px;height:30px;margin-top:10px;display:block;background:red;"></div>
<div id="greenDiv" style="width:30px;height:30px;margin-top:10px;display:block;background:green;"></div>

My not edited Jquery http://jsfiddle.net/gDChA/23/ :我没有编辑 Jquery http://jsfiddle.net/gDChA/23/

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',
          'class' : 'infoinput'
        });    
        $(this).parent().append(element);
    })
    $('button.remove').click ( function(e) {
        $(this).parent().find('button.add').show();
        $(this).hide();
        //findLastInput(this).remove('input');
        $(this).parent().find("input").remove();
    });

First I have tried to add the extra HTML without luck.首先,我尝试添加额外的 HTML 没有运气。 I have edited this $(this).parent().append(element);我已经编辑了这个$(this).parent().append(element);

To this and it is not working:为此,它不起作用:

$(this).parent().append(element + '<div id="redDiv" style="width:30px;height:30px;margin-top:10px;display:block;background:red;"></div>
    <div id="greenDiv" style="width:30px;height:30px;margin-top:10px;display:block;background:green;"></div>');

I want to add the HTML and removed it the same way as the input field works.我想添加 HTML 并以与输入字段相同的方式将其删除。

You proably need to add your extra html to element.html() instead of element (element is just an object returned from your function findLastInput), so it should be something like this:您可能需要将额外的 html 添加到 element.html() 而不是 element (元素只是从您的 function findLastInput 返回的 object),所以应该是这样的:

$(this).parent().append( element.html() + ''); $(this).parent().append( element.html() + '');

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

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