所以我做了一個表格元素和功能彈出和窗體。單擊保存按鈕上的追加元素也可以使用。然而,在新的彈出窗口中,無論單元格是滿的還是空的,表單中的數據都會附加到每個以前單擊的表格單元格中。我以某種方式試圖用當前生成的ID填充單元格。考慮到我是JavaScript新手的事實,我完全錯過了一些東西。有人可以告訴我這是什麼。該守則如何從彈出窗體中追加當前點擊過的表格元素中的元素?
//================ADDs POPUP ON CLICK================/
$(document).ready(function() {
/*Adding the klikanje class to td*/
$('table tbody tr:not(:first) td').addClass('klikanje');
/*removing the klikanje class from the first column*/
$('table tr:first-child, table td:first-child').removeClass('klikanje');
/*removing the klikanje class from the first row*/
$('table tbody tr:first-child td').removeClass('klikanje');
/*Making random id*/
/*appending data atributs to empty td*/
$('.klikanje').click(function(){
var self = $(this);
if ($(this).is(':empty')) {
var idBase = 'clickId-';
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
var idNumber = getRandomInt(1, 1000000);
var clickID = idBase + idNumber
var callingID = '#' + clickID;
$(this).attr({ 'data-toggle': 'modal', 'data-target': '#popUp', 'id': clickID });
/*save to td */
$('#save').click(function() {
var theName = $('input[name=name]').val();
var theLastName = $('input[name=lastname]').val();
var $fullCell = $('<p>' + theName + '' + theLastName + '</p>');
if((theLastName+theLastName).length > 0){
$(callingID).append($fullCell);
$(callingID).css('background-color', 'yellow');
}
}); /*save to td end */
} else {
alert('Already taken spot. Please pick another one!');
$(this).attr({ 'data-toggle': '', 'data-target': '', 'id': '' });
}
});
});//<---ADDs POPUP ON CLICK END
全碼:JsFiddle
這也是有用謝謝你,但那不是我的問題。當我點擊新的單元格,並且在輸入輸入值之後,這些值被添加到當前點擊的單元格和之前單擊的每個其他單元格中。我不知何故試圖用當前生成的ID填充單元格。 使用您的代碼,只有在第一次單擊的單元格中才會添加新的輸入值。檢查[這裏](https://jsfiddle.net/9zcj3ab8/24/)。 –
該行爲正在發生,因爲你的調用ID是局部變量來點擊功能,我已經更新了答案。 – Deep
還有一個問題。如何將其保存到本地存儲中,並且在頁面刷新時,輸入值出現在單元格中? –