HTML可编辑表:添加包括单选按钮的新行

时间:2019-11-22 07:09:03

标签: javascript jquery html contenteditable

比方说,我有一个可编辑的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>
  

您可以看到第一列具有带有 group1 组名称的单选按钮。

我要说的是,下次单击添加新行按钮时,新行应该

  • 包括与新的组名称组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>
    

2 个答案:

答案 0 :(得分:1)

希望这会有所帮助

HTML

import { Keyboard } from '@ionic-native/keyboard';

constructor(private keyboard: Keyboard) { }

...

this.keyboard.show();

this.keyboard.hide();

JS

<table id="exp02Table" class="responsive-table table table-bordered">
  <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 class="addN">
 Add New
</button>

工作Fiddle

答案 1 :(得分:1)

为了在与某种元素(例如按钮)交互之后向您的文档对象模型(DOM)添加新对象,可能需要一些JavaScript。请参见以下小提琴:

使用普通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>

我在您的DOM中添加了<button onclick="AddRow()">Add row</button>(在HTML中),并添加了一个带有功能Addrow()的脚本(请参阅小提琴以获取参考)。

在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>