-1

我有一个使用 jQuery 单击按钮动态添加到页面的 Div。

我希望能够使用该RemoveArtist()功能通过单击 cross-button.png 来删除 Div。

$("<div class=\"input-append\" id=\"artist"
    + artists.length - 1 + "\" ><label style=\"background-color:#e0ffff\">"
    + artistVal + "</label><img onclick=\"RemoveArtist()\" id=\"removeartist\""
    + " src=\"/Content/bootstrap/img/cross-button.png\" /></div>")
    .appendTo(addDiv);

移除艺术家功能应如下所示:

var RemoveArtist = function (div,artistlength) {

    $('div').remove();
    artists.splice(artistlength, 1);
};

如何将实际的 div 传递给RemoveArtist 函数,以及artistlength哪个是artists.length - 1div 的值?

所以基本上我试图删除 Div 并从艺术家数组中删除艺术家。

4

1 回答 1

1

在我看来,如果你创建实际元素而不是字符串,你会更好。
写的东西更多,但让一切都更容易跟踪:

var input_append = $('<div />', {id: 'artist' + (artists.length - 1)}),
    label        = $('<label />', {style: 'background-color:#e0ffff',
                                   text : artistVal
                                  }
                    ),
    image        = $('<img />', {id:  'removeartist',
                                 src: '/Content/bootstrap/img/cross-button.png',
                                 on: {
                                       click: function() {
                                          input_append.remove();
                                          artists.splice(artists.length - 1, 1);
                                       }
                                     }
                                }
                    );

addDiv.append( input_append.append(label, img) );
于 2013-09-01T22:12:00.390 回答