【问题标题】:Jquery help append HTML not workingJquery帮助追加HTML不起作用
【发布时间】:2011-08-15 14:48:00
【问题描述】:

我也在尝试在添加输入字段时添加此 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>

我未编辑的 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();
    });

首先,我尝试添加额外的 HTML,但没有运气。我已经编辑了这个$(this).parent().append(element);

对此,它不起作用:

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

我想添加 HTML 并以与输入字段相同的方式将其删除。

【问题讨论】:

    标签: javascript jquery html jquery-ui jquery-selectors


    【解决方案1】:

    您可能需要将额外的 html 添加到 element.html() 而不是 element (元素只是从您的函数 findLastInput 返回的一个对象),所以它应该是这样的:

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

    【讨论】:

    • 代码试图做什么?这一行是什么“var element = findLastInput(this).clone();”试图克隆?
    • 它确实克隆了输入字段
    • 有 1 个空格的问题,现在添加了 HTML:jsfiddle.net/gDChA/44
    猜你喜欢
    • 2011-11-10
    • 2014-04-15
    • 2019-02-14
    • 1970-01-01
    • 2010-11-03
    • 2016-09-02
    • 2012-04-05
    • 1970-01-01
    • 2014-01-05
    相关资源
    最近更新 更多