我有一个包含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);
}
}
});
});
答案 0 :(得分:2)
动态添加的按钮不会绑定到任何内容。
解决方案是让你的onload绑定成为一个你可以随时调用的函数......
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';
});
}
});
}
加载时绑定按钮。
$(document).ready(function() {
// Bind the deactivate button click to the function
bindDeactivate();
});
在添加行功能结束时,绑定按钮。
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">
。
我在HTML中为<input type="button">
更改了它们
并为$this.text(function(i, t) {
更改了$this.val(function(i, t) {
我还改进了双击效果&#34;在重新绑定之前解除绑定...在addRow(tableID)
函数的末尾。
// Bind this new deactivate button click to the function
$('#html_master').off("click",'.deactivate');
bindDeactivate();
请再次重新检查我的codePen
它现在正在工作(仅用于取消激活/激活按钮)
您可以为“编辑”按钮应用相同的逻辑...
;)
答案 1 :(得分:0)
bocz你在加载时绑定事件,所以它适用于现有数据行,但新添加的行没有任何绑定,请尝试下面的代码绑定事件。
$(document).on('click','.deactivate',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';
});
}
});
和编辑
$(document).on('click','.edit',function() {
var $this = $(this);
var tds = $this.closest('tr').find('td').not('.mr_id').filter(function() {
return $(this).find('.edit').length === 0;
});
});
答案 2 :(得分:0)
使用代码
$(document).on('click','.deactivate',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';
});
}
});
$(document).on('click','.edit',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);
}
}
});
答案 3 :(得分:0)
新添加的行不响应
edit
和deactivate
方法的主要原因是它们是动态创建的,因此未绑定到最初声明为onclick
个事件的那些其他。您可能必须在创建行后简单地附加这些事件。下面的代码段试图说明如何。
注意:
下面的代码段结合了JQueryready
方法下的所有Javascript代码因为在一个脚本中不需要单独的ready
方法。其次,要创建新的Row,Jquery(而不是使用Raw Javascript)。为简单起见。
<强> HTML 强>
<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>
</table>
<div class="add-button-wrapper">
<input type="button" class="add" value="Add Row" id="rowAdder">
</div>
JAVASCRIPT:JQUERY
<script type="text/javascript">
$(document).ready(function() {
var rowAdder = $("#rowAdder");
var editButton = $(".edit");
var deActivator = $('.deactivate');
/* EVENT BINDINGS & HANDLING*/
// BIND THE CLICK ACTION TO DEACTIVATION HANDLER
deActivator.click(deactivate);
// BIND THE CLICK EVENT TO THE EDITING HANDLER:
editButton.click(handleEdit);
// BIND THE CLICK ACTION TO THE NEW-ROW PROCESSING HANDLER:
rowAdder.click(addRow);
/* CALL-BACK FUNCTIONS:*/
// HANDLE THE EDITING
function handleEdit(evt) {
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);
}
}
}
// HANDLE DEACTIVATION
function deactivate(evt) {
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 NEW ROW
function addRow(evt) {
var table = $("#html_master");
var rows = table.children("tr");
var rowCount = rows.length;
var row = table.insertRow(rowCount);
// SIMPLY CREATE A NEW ROW OBJECT AND APPEND IT TO THE rows VARIABLE.
var newRow = "<tr>";
newRow += "<td class='mr_id' contenteditable='false'>" + rowCount + "</td>";
newRow += "<td class='buyer_id' contenteditable='false'><input type='text' name='txtbox[]' />" + "</td>";
newRow += "<td class='poc_n' contenteditable='false'><input type='text' name='txtbox[]' />" + "</td>";
newRow += "<td class='poc_e' contenteditable='false'><input type='text' name='txtbox[]' />" + "</td>";
newRow += "<td class='poc_p' contenteditable='false'><input type='text' name='txtbox[]' />" + "</td>";
newRow += "<td class='mr_id' contenteditable='false'><input type='text' name='txtbox[]' />" + "</td>";
newRow += "<td class='mr_id' contenteditable='false'>";
newRow += "<button class='edit' name='edit'>Edit</button>";
newRow += "<button class='deactivate' name='deactivate'>Deactivate</button>";
newRow += "</td>";
newRow += "</tr>";
// APPEND THE NEW ROW...
table.append(newRow);
// RE-BIND THE EVENTS ON THE BUTTONS SINCE THESE WERE DYNAMICALLY CREATED
// BIND THE CLICK ACTION TO DEACTIVATION HANDLER
table.find("deactivate").click(deactivate);
// BIND THE CLICK EVENT TO THE EDITING HANDLER:
table.find("edit").click(handleEdit);
}
});
</script>
答案 4 :(得分:-1)
尝试使用 id 代替类,如:
<td><button class="edit" id="edit" name="edit">Edit</button>
<button class="deactivate" id="deactivate" name="deactivate">Deactivate</button></td>
在jQuery中,替换&#34; .edit&#34;通过&#34;#edit&#34;和&#34; .deactivate&#34;通过&#34; #activate&#34;。