2
我有這個js代碼添加(克隆)並刪除一個元素。刪除select,從克隆元素
$('#btnAdd1').click(function (event) {
var num = $('.linguas').length;
var newNum = new Number(num + 1);
var newElem = $('#input_' + num).clone(false).prop('id', 'input_' + newNum);
newElem.children(':text').val('');
$('#input_' + num).after(newElem).find('#input_' + num +'> option:selected').removeAttr("selected");
$('#btnDel1').prop('disabled', '');
if (newNum == 4) $('#btnAdd1').prop('disabled', 'disabled');
});
不過,我想刪除select="select"
屬性,從以前的元素克隆。
我想這樣的事情,但沒有奏效:
$('#input_' + num).after(newElem).find('#input_' + num +'> option:selected').removeAttr("selected");
演示here
感謝
謝謝,完美:) – Daniel 2011-12-20 16:26:25
@Daniel如果您使用的是jQuery> = 1.6版本,請使用'.removeProp()'而不是'.removeAttr()'。 – JesseBuesking 2011-12-20 16:29:57
@JesseB - 你確定嗎?這是來自removeProp文檔'注意:不要使用此方法刪除本機屬性,如選中,禁用或選中。這將完全刪除該屬性,一旦刪除,不能再添加到元素。使用.prop()將這些屬性設置爲false。「# – 2011-12-20 16:32:12