使用jquery单击按钮上的可编辑引导程序表行



我正在尝试使用Jquery编辑按钮单击时引导表的值。我从这篇文章中获得了参考资料,使行可编辑而且效果非常好。我遇到的唯一问题是双击按钮进入编辑模式。这是我的代码片段

$('.editbtn').click(function () {
var currentTD = $(this).parents('tr').find('td');
if ($(this).html() == 'Edit') {
currentTD = $(this).parents('tr').find($("td").not(":nth-child(1)"));
$.each(currentTD, function () {
$(this).prop('contenteditable', true).css({
'background':'#fff',
'color':'#000'
})
});
} else {
$.each(currentTD, function () {
$(this).prop('contenteditable', false).removeAttr("style");
});
}

$(this).html($(this).html() == 'Edit' ? 'Save' : 'Edit')
if ($(this).html() == 'Save'){
$(this).prop('contenteditable',false)
}

});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<table id="tableone" class="table table-striped table-dark table-bordered" border="1">
<thead>
<tr>
<th scope="col">Date</th>
<th scope="col">Name</th>
<th scope="col">Status</th>
</tr>
</thead>

<tbody>
<tr>
<td>21st August</td>
<td>abc</td>

<td  contenteditable="false"><button type="button"class="btn btn-primary editbtn">Edit</button>
<button type="button" class="btn btn-danger">
Delete
</button></td>
</tr>
<tr>
<td>21st August</td>
<td>abc</td>

<td  contenteditable="false"><button type="button" class="btn btn-primary editbtn" >
Edit
</button>
<button type="button" class="btn btn-danger">
Delete
</button></td>
</tr>  
<tr>
<td>21st August</td>
<td>abc</td>

<td  contenteditable="false"><button type="button" class="btn btn-primary editbtn" >
Edit
</button>
<button type="button" class="btn btn-danger">
Delete
</button></td>
</tr>  <tr>
<td>21st August</td>
<td>abc</td>
<td  contenteditable="false"><button type="button" class="btn btn-primary editbtn">
Edit
</button>
<button type="button" class="btn btn-danger">
Delete
</button></td>
</tr>
</tbody>
</table>

在这里,我禁用了编辑一列中的数据。正如您所看到的,当我们单击第一行中的编辑按钮时,只需要单击一次,但对于其余行,则需要多次单击。在多次单击后,它将处于编辑模式下单击,直到刷新页面。我不知道我错过了哪里。我将感谢你的帮助。

问题是这一行:在执行$(this).html()时,它考虑了line

<button type="button" class="btn btn-primary editbtn" >
Edit
</button>

为什么它在下次点击时起作用,因为在您的代码中,您更改为编辑

$(this).html($(this).html() == 'Edit' ? 'Save' : 'Edit')

您可以尝试以下操作:

使用$(this).text().trim()或更正行:

<button type="button" class="btn btn-primary editbtn" >Edit</button>

<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<script>
$(document).ready(function(){
$('.editbtn').click(function () {
var currentTD = $(this).parents('tr').find('td');
//alert('this '+$(this).html() );
if ($(this).text().trim() == 'Edit') {
currentTD = $(this).parents('tr').find($("td").not(":nth-child(1)"));
//alert("first if "+currentTD.html());
$.each(currentTD, function () {
$(this).prop('contenteditable', true).css({
'background':'#fff',
'color':'#000'
})
});
} else {
// alert("second if "+currentTD.html());
$.each(currentTD, function () {
//alert("second if "+currentTD.html());
$(this).prop('contenteditable', false).removeAttr("style");
});
}

$(this).html($(this).html() == 'Edit' ? 'Save' : 'Edit')
if ($(this).html() == 'Save'){
// alert("third if "+currentTD.html());
$(this).prop('contenteditable',false)
}

});
});
</script>
</head>
<body>
<table id="tableone" class="table table-striped table-dark table-bordered" border="1">
<thead>
<tr>
<th scope="col">Date</th>
<th scope="col">Name</th>
<th scope="col">Status</th>
</tr>
</thead>

<tbody>
<tr>
<td>21st August</td>
<td>abc</td>

<td  contenteditable="false"><button type="button"class="btn btn-primary editbtn">Edit</button>
<button type="button" class="btn btn-danger">
Delete
</button></td>
</tr>
<tr>
<td>21st August</td>
<td>abc</td>

<td  contenteditable="false"><button type="button" class="btn btn-primary editbtn" >Edit</button>
<button type="button" class="btn btn-danger">
Delete
</button></td>
</tr>  
<tr>
<td>21st August</td>
<td>abc</td>

<td  contenteditable="false"><button type="button" class="btn btn-primary editbtn" >Edit</button>
<button type="button" class="btn btn-danger">
Delete
</button></td>
</tr>  <tr>
<td>21st August</td>
<td>abc</td>
<td  contenteditable="false"><button type="button" class="btn btn-primary editbtn">Edit</button>
<button type="button" class="btn btn-danger">
Delete
</button></td>
</tr>
</tbody>
</table>
</body>
</html>

最新更新