在页面加载时会列出一些数据。我的目的是在用户点击跨跨时从数据库中删除每个数据。我添加了一个十字按钮,它的类名是“removefetchedData”。目前我的问题是当用户点击十字架时,只显示一个ID。
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">×</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
var delID = 0;
$.each(responseDB.selectLanguageRankingTagId, function (i, item) {
delID = item.id;
// 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><span class="glyphicon glyphicon-remove removefetchedData" data-removefile=""></span></div></div>')
// set the select value
$newRow.find('select').val(item.ranking);
// add the row to the modal body
$modalBody.append($newRow);
});
//remove rank and language of users
$( ".removefetchedData" ).click(function(e){
e.preventDefault();
alert(delID);
/*$.post("/language/delete", {rateUserLangID: userLangDelID}, function(html){
//append css design
});*/
});
$(".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>');
});
$('.modal-dialog').on('click', '.removefile', function() {
$(this).closest('.row').remove();
});
//});// get languages end
HTML
<div id="languagemodal">
<div class="modal-dialog" style="margin: 54px 0px;"></div>
</div>
答案 0 :(得分:0)
由于你没有给id删除按钮,你可以这样做:
$( ".removefetchedData" ).click(function(e){
e.preventDefault();
alert($(this).parents('.row').find('.input-sm').attr('id'));
});
的jsfiddle:
http://jsfiddle.net/Lr6a5d2v/18/
上述方法有效,并为您提供id
,您曾使用select box
,因此我们可以使用该ID将其发送到php file
以进一步处理。
以及我能看到的其他内容是你在javascript中制作交叉(删除按钮)的html,这样你也可以像给按钮一样给出一个data-id =“”然后在javascript中使用那个id:< / p>
<span class="glyphicon glyphicon-remove removefetchedData" data-removefile="" data-id="'+item.id+'"></span>
JS
$( ".removefetchedData" ).click(function(e){
e.preventDefault();
alert($(this).data('id'));
});
小提琴: