- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有一个可编辑的 html 表格。在这些表格单元格之一中,它包含单选按钮。我想要的是添加一个新行按钮。当我单击按钮时,新行应包含与新组相同的单选按钮。
比方说,当页面最初加载时,表格类似于:
<table id="exp02Table" class="responsive-table">
<thead>
<tr class="bengaliText">
<th> (P)</th>
<th> (a cm)</th>
<th> (b cm)</th>
<th> (c cm)</th>
<th> L=(a-b) cm</th>
<th> H = (c-b) cm </th>
<th> P = H±h</th>
<th> PV = (H±h)*L</th>
</tr>
</thead>
<tbody id="exp02TBody">
<tr>
<td>
<p> <label> <input value="0" checked name="group1" type="radio" /> <span>বায়ু মন্ডলের চাপ</span> </label> </p>
<p> <label> <input value="1" name="group1" type="radio" /> <span>বায়ু মন্ডলের চাপের বেশী</span> </label> </p>
<p> <label> <input value="2" name="group1" type="radio" /> <span>বায়ু মন্ডলের চাপের কম</span> </label> </p>
</td>
<td contenteditable></td>
<td contenteditable></td>
<td contenteditable></td>
<td contenteditable></td>
<td contenteditable></td>
<td contenteditable></td>
<td contenteditable></td>
</tr>
</tbody>
</table>
You can see the first column has radio buttons with group1 group name.
我想说的是,下次当我单击“添加新行”按钮时,新行应该
包含具有新组名称组 2、组 3 .... 的相同单选按钮,并且应选中第一个单选选项
和其他td(表数据)应该是内容可编辑的,没有其中的值:
<tr>
<td>
<p> <label> <input value="0" checked name="group2" type="radio" /> <span>বায়ু মন্ডলের চাপ</span> </label> </p>
<p> <label> <input value="1" name="group2" type="radio" /> <span>বায়ু মন্ডলের চাপের বেশী</span> </label> </p>
<p> <label> <input value="2" name="group2" type="radio" /> <span>বায়ু মন্ডলের চাপের কম</span> </label> </p>
</td>
<td contenteditable></td>
<td contenteditable></td>
<td contenteditable></td>
<td contenteditable></td>
<td contenteditable></td>
<td contenteditable></td>
<td contenteditable></td>
</tr>
最佳答案
为了在与某种元素(例如按钮)交互后向文档对象模型 (DOM) 添加新对象,可能需要一些 JavaScript。请参阅以下 fiddle :
在纯 JavaScript 中:
let i = 1;
function AddRow() {
i++;
let tbody = document.getElementById("exp02TBody");
let tr = document.createElement("tr");
//The HTML for the row. Obviously not as readable in JavaScript so there might a bit cleaner solution to this but at least it's functional:
tr.innerHTML =
"<td><p><label><input value='0' checked name='group-" + i + "' type='radio'> বায়ু মন্ডলের চাপ</label></p><p><label><input value='1' name='group-" + i + "' type='radio'> বায়ু মন্ডলের চাপের বেশী</label></p><p><label><input value='2' name='group-" + i + "' type='radio'> বায়ু মন্ডলের চাপের কম</label></p></td><td contenteditable></td><td contenteditable></td><td contenteditable></td><td contenteditable></td><td contenteditable></td><td contenteditable></td><td contenteditable></td>";
tbody.appendChild(tr);
}
<table id="exp02Table" class="responsive-table">
<thead>
<tr class="bengaliText">
<th> (P)</th>
<th> (a cm)</th>
<th> (b cm)</th>
<th> (c cm)</th>
<th> L=(a-b) cm</th>
<th> H = (c-b) cm </th>
<th> P = H±h</th>
<th> PV = (H±h)*L</th>
</tr>
</thead>
<tbody id="exp02TBody">
<tr>
<td>
<p> <label> <input value="0" checked name="group1" type="radio" /> <span>বায়ু মন্ডলের চাপ</span> </label> </p>
<p> <label> <input value="1" name="group1" type="radio" /> <span>বায়ু মন্ডলের চাপের বেশী</span> </label> </p>
<p> <label> <input value="2" name="group1" type="radio" /> <span>বায়ু মন্ডলের চাপের কম</span> </label> </p>
</td>
<td contenteditable></td>
<td contenteditable></td>
<td contenteditable></td>
<td contenteditable></td>
<td contenteditable></td>
<td contenteditable></td>
<td contenteditable></td>
</tr>
</tbody>
</table>
<button onclick="AddRow()">Add row</button>
我添加了<button onclick="AddRow()">Add row</button>
到你的 DOM(在 HTML 中),我添加了一个带有函数 Addrow()
的脚本(请参阅 fiddle 以供引用)。
在 jQuery 中:
$(document).ready(function() {
let i = 1;
$("#row-button").on("click", function() {
i++;
$("#exp02TBody").append("<tr><td><p><label><input value='0' checked name='group-" + i + "' type='radio'> বায়ু মন্ডলের চাপ</label></p><p><label><input value='1' name='group-" + i + "' type='radio'> বায়ু মন্ডলের চাপের বেশী</label></p><p><label><input value='2' name='group-" + i + "' type='radio'> বায়ু মন্ডলের চাপের কম</label></p></td><td contenteditable></td><td contenteditable></td><td contenteditable></td><td contenteditable></td><td contenteditable></td><td contenteditable></td><td contenteditable></td></tr>");
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="exp02Table" class="responsive-table">
<thead>
<tr class="bengaliText">
<th> (P)</th>
<th> (a cm)</th>
<th> (b cm)</th>
<th> (c cm)</th>
<th> L=(a-b) cm</th>
<th> H = (c-b) cm </th>
<th> P = H±h</th>
<th> PV = (H±h)*L</th>
</tr>
</thead>
<tbody id="exp02TBody">
<tr>
<td>
<p> <label> <input value="0" checked name="group1" type="radio" /> <span>বায়ু মন্ডলের চাপ</span> </label> </p>
<p> <label> <input value="1" name="group1" type="radio" /> <span>বায়ু মন্ডলের চাপের বেশী</span> </label> </p>
<p> <label> <input value="2" name="group1" type="radio" /> <span>বায়ু মন্ডলের চাপের কম</span> </label> </p>
</td>
<td contenteditable></td>
<td contenteditable></td>
<td contenteditable></td>
<td contenteditable></td>
<td contenteditable></td>
<td contenteditable></td>
<td contenteditable></td>
</tr>
</tbody>
</table>
<button id="row-button">Add row</button>
关于javascript - HTML 可编辑表 : Add new row including radio buttons,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58989191/
我遇到过这个 html: 上面的html和这个有什么区别: 最佳答案 来自MDN page on the tag : 对于 type 的属性标签,可能的值是: 提交:按钮将表单数据提交给服务器
Button button= (Button) findViewbyID(R.id.button); 和 Button button = new Button(this); 有什么区别? 最佳答案 有
我是一名优秀的程序员,十分优秀!