我正在尝试使用Jquery在单击按钮时编辑引导表的值。我已从本帖子making row editable中引用 并能正常工作。我唯一的问题是双击按钮进入编辑模式。这是我的代码段
list1 := &list{head: new(Node)}
// ...
list2 := &list{head: new(Node)}
// ...
$('.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)
}
});
这里我禁用了从一列中编辑数据。如您所见,当我们单击第一行中的“编辑”按钮时,只需要单击一次,而其余行则需要多次单击。 ,只需单击一下即可进入编辑模式,直到刷新页面。我不知道我错过了什么。感谢您的帮助。
答案 0 :(得分:1)
问题是这一行:您进行line
时会考虑 $(this).html()
。
<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>