gpt4 book ai didi

javascript - 添加表格行时按钮不起作用

转载 作者:搜寻专家 更新时间:2023-10-31 08:52:07 25 4
gpt4 key购买 nike

我有一个表,其中有一列有 2 个按钮,编辑和停用。两者都完美运行。但是,我在表外还有一个按钮,用于向表中添加一行。在所有添加的行上,我的编辑和停用按钮都不起作用。我该如何解决这个问题?

HTML/PHP:

<table id="html_master">
<thead>
<tr>
<td>ID</td>
<td>Vendor</td>
<td>Buyer ID</td>
<td>POC Name</td>
<td>POC Email</td>
<td>POC Phone</td>
<td>Edit/Delete</td>
</tr>
</thead>
<tbody>

<?php
foreach ($dbh->query($sql) as $rows){
?>
<tr>
<td class="mr_id" contenteditable="false"><?php echo intval ($rows['MR_ID'])?></td>
<td class="mr_name" contenteditable="false"><?php echo $rows['MR_Name']?></td>
<td class="buyer_id" contenteditable="false"><?php echo $rows['Buyer_ID']?></td>
<td class="poc_n" contenteditable="false"><?php echo $rows['MR_POC_N']?></td>
<td class="poc_e" contenteditable="false"><?php echo $rows['MR_POC_E']?></td>
<td class="poc_p" contenteditable="false"><?php echo $rows['MR_POC_P']?></td>
<td><button class="edit" name="edit">Edit</button>
<button class="deactivate" name="deactivate">Deactivate</button></td>
</tr>
<?php
}
?>
</tbody>
<br>
<input type="button" class="add" value="Add Row" onclick="addRow('html_master')">
</table>

JavaScript:

// ----- Deactivate Row -----

$(document).ready(function() {
$('.deactivate').click(function() {
var $this = $(this);
var $tr = $this.closest('tr');
var action = $tr.hasClass('deactivated') ? 'activate' : 'deactivate';

if (confirm('Are you sure you want to ' + action + ' this entry?')) {
$tr.toggleClass('deactivated');
$this.text(function(i, t) {
return t == 'Deactivate' ? 'Activate' : 'Deactivate';
});
}
})
});

// ----- Add Row -----

function addRow(tableID) {

var table = document.getElementById(tableID);

var rowCount = table.rows.length;
var row = table.insertRow(rowCount);

var cell1 = row.insertCell(0);
cell1.innerHTML = rowCount;

var cell2 = row.insertCell(1);
var element2 = document.createElement("input");
element2.type = "text";
element2.name = "txtbox[]";
cell2.appendChild(element2);

var cell3 = row.insertCell(2);
var element3 = document.createElement("input");
element3.type = "text";
element3.name = "txtbox[]";
cell3.appendChild(element3);

var cell4 = row.insertCell(3);
var element4 = document.createElement("input");
element4.type = "text";
element4.name = "txtbox[]";
cell4.appendChild(element4);

var cell5 = row.insertCell(4);
var element5 = document.createElement("input");
element5.type = "text";
element5.name = "txtbox[]";
cell5.appendChild(element5);

var cell6 = row.insertCell(5);
var element6 = document.createElement("input");
element6.type = "text";
element6.name = "txtbox[]";
cell6.appendChild(element6);

var cell7 = row.insertCell(6);
var element7 = document.createElement("input");
var element8 = document.createElement("input");
element7.type = "button";
element8.type = "button";
element7.name="edit";
element8.name="deactivate";
element7.value="Edit";
element8.value="Deactivate";
cell7.appendChild(element7);
cell7.appendChild(element8);
}

$(document).ready(function() {
$('.edit').click(function() {
var $this = $(this);
var tds = $this.closest('tr').find('td').not('.mr_id').filter(function() {
return $(this).find('.edit').length === 0;
});
if ($this.html() === 'Edit') {
$this.html('Save');
tds.prop('contenteditable', true);
} else {
var isValid = true;
var errors = '';
$('#myDialogBox').empty();
tds.each(function(){
var type = $(this).attr('class');
var value = $(this).text();
switch(type){
case "buyer_id":
if(!$.isNumeric(value)){
isValid = false;
errors += "Please enter a valid Buyer ID\n";
}
break;
case "poc_n":
if(value == value.match(/^[a-zA-Z\s]+$/)){
break;
}
else {
isValid = false;
errors += "Please enter a valid Name\n";
}
break;
case "poc_e":
if(value == value.match(/^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/)){
break;
}
else {
isValid = false;
errors += "Please enter a valid Email\n";
}
break;
case "poc_p":
if(value == value.match('^[0-9 ()+/-]{10,}$')){
break;
}
else {
isValid = false;
errors += "Please enter a valid Phone Number\n";
}
break;
}
})
if(isValid){
$this.html('Edit');
tds.prop('contenteditable', false);
}else{
alert(errors);
}
}
});
});

最佳答案

动态添加的按钮不绑定(bind)任何东西。

解决方案是使您的 onload 绑定(bind)成为一个您可以随时调用的函数...

function bindDeactivate() {
$('.deactivate').click(function() {
var $this = $(this);
var $tr = $this.closest('tr');
var action = $tr.hasClass('deactivated') ? 'activate' : 'deactivate';

if (confirm('Are you sure you want to ' + action + ' this entry?')) {
$tr.toggleClass('deactivated');
$this.text(function(i, t) {
return t == 'Deactivate' ? 'Activate' : 'Deactivate';
});
}
});
}

在加载时绑定(bind)按钮。

$(document).ready(function() {
// Bind the deactivate button click to the function
bindDeactivate();
});

在添加行函数的末尾,绑定(bind)按钮。

function addRow(tableID) {
// Skipped a couple lines...

cell7.appendChild(element7);
cell7.appendChild(element8);

// Bind this new deactivate button click to the function
bindDeactivate();
}

您将能够对编辑按钮执行相同的操作...
;)

编辑

你有另一个问题。
您动态添加的按钮没有关联的类。

将此添加到您的 function addRow(tableID)将类添加到“停用”按钮。

var setClass = document.createAttribute("class");
setClass.value = "deactivate";
element8.setAttributeNode(setClass);

对编辑按钮做同样的事情。

查看我的 CodePen here .

编辑

好的...您遇到的另一个问题:
在您的初始 HTML 中,按钮是 <button>标签。
而动态添加的是<input type="button">

这会导致 $this.text(function(i, t) {不在 <input type="button"> 上工作.

我将它们更改为 <input type="button">在您的 HTML 中。
并更改了$this.text(function(i, t) {对于 $this.val(function(i, t) {

我还通过在重新绑定(bind)之前解除绑定(bind)来改进“双击效果”......在addRow(tableID)的末尾功能。

// Bind this new deactivate button click to the function
$('#html_master').off("click",'.deactivate');
bindDeactivate();

请再次检查我的codePen
它现在正在工作(仅适用于停用/激活按钮)
您可以对“编辑”按钮应用相同的逻辑...
;)

关于javascript - 添加表格行时按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39854971/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com