gpt4 book ai didi

javascript - 如何使用 jQuery 删除一行?

转载 作者:行者123 更新时间:2023-12-03 08:23:33 29 4
gpt4 key购买 nike

页面底部列出了一个下拉列表,用于选择新语言“添加语言”。如果用户选择新语言,将生成一行。如果他们不需要,可以将其删除。我添加了一个十字按钮并添加了一个类名“removefile”。它在我当前的代码中如何工作?

Demo

JS

    //fake data for this test
var response = {
availableLanguage: [{
id: "1",
language_id: "English",
title_en: "USEnglish"
}, {
id: "2",
title_en: "Hindi"
}, {
id: "3",
title_en: "Arabi"
}

]
}


var responseDB = {
selectLanguageRankingTagId: [{
id: "1",
user_id: "11",
language_id: "English",
ranking: "2",
title_en: "English"
}, {
id: "2",
user_id: "11",
language_id: "German",
ranking: "3",
title_en: "German"
}, {
id: "3",
user_id: "11",
language_id: "French",
ranking: "4",
title_en: "French"
}

]
}


var $languagemodal = $('#languagemodal'); // get the modal and the dialog div
var $dialog = $languagemodal.find('.modal-dialog');

//$languagemodal.modal({show: true}); // just to show the modal for the demo

// get languages
//$.get("/tag/language", function(response){
var optionLang = '';
for (var i = 0; i < response.availableLanguage.length; i++) {
engLangID = response.availableLanguage[i].id;
engLang = response.availableLanguage[i].title_en;
optionLang += '<option value="'+engLangID+'" data-language="'+engLang+'">'+engLang+'</option>';
}

// load the modal content div
$dialog.html('<div class="modal-content"><div class="modal-header "><button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button><h4 class="modal-title">Edit Languages</h4></div><div class="modal-body"><div class="appendRow"></div></div><div class="modal-footer"><div class="row"><select class="form-control input-sm addMore"><option selected="selected">Add Language</option>' + optionLang + '</select></div><br><button type="button" class="btn btn-default" data-dismiss="modal">Close</button><button type="button" class="btn btn-primary">Save changes</button></div></div>');

// get the modal body we just added
var $modalBody = $dialog.find('.modal-body');

// loop over your data, you would have this in your $.get function
$.each(responseDB.selectLanguageRankingTagId, function (i, item) {
// make the row
var $newRow = $('<div class="row"><div class="col-md-8"><h4 style="background-color: lightgrey; border-radius: 10px; background-repeat: repeat; height: 30px; margin:5px 0px; padding:4px 5px;">' + item.title_en + '</h4></div><div class="col-md-3" style="padding:5px;"><select class="form-control input-sm" id="' + item.id + '" data-item="' + item + '"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option></select></div></div>')
// set the select value
$newRow.find('select').val(item.ranking);
// add the row to the modal body
$modalBody.append($newRow);

});

$(".modal-dialog").on('change', ".addMore", function(){
var selected = $( this ).find(':selected');
language = selected.data('language');
languageID = selected.val();
$( ".appendRow" ).append('<div class="row"><div class="col-md-8"><h4 style="background-color: lightgrey; border-radius: 10px; background-repeat: repeat; height: 30px; margin:5px 0px; padding:4px 5px;">'+language+'</h4></div><div class="col-md-3" style="padding:5px;"><select class="form-control input-sm"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option></select><span class="glyphicon glyphicon-remove removefile" data-removefile=""></span></div></div>');
});

//});// get languages end

HTML

<div id="languagemodal">
<div class="modal-dialog" style="margin: 54px 0px;"></div>
</div>

最佳答案

您需要为 .removefile 元素添加另一个委托(delegate)事件处理程序,以便删除父 .row 元素。试试这个:

$('.modal-dialog').on('click', '.removefile', function() {
$(this).closest('.row').remove();
});

Updated fiddle

关于javascript - 如何使用 jQuery 删除一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33649346/

29 4 0
文章推荐: javascript - Jquery获取所选选项下拉列表的文本
文章推荐: javascript - PHP URL 变量转换为 Javascript
文章推荐: javascript - 如何向 WebUI-Popover jQuery 插件添加确认按钮?
文章推荐: javascript - 当 mouseenter 触发时,在 标记内的选定 SVG 中添加属性