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

比方说,我有一个可编辑的 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 (表数据)应该是 contenteditable ,其中没有值:


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


回首忆惘然
浏览 206回答 2
2回答

慕少森

为了在与某种元素(例如按钮)交互后向文档对象模型 (DOM) 添加新对象,可能需要一些 JavaScript。请参阅以下小提琴:在纯 JavaScript 中:let i = 1;function AddRow() {&nbsp; i++;&nbsp; let tbody = document.getElementById("exp02TBody");&nbsp; let tr = document.createElement("tr");&nbsp;&nbsp;&nbsp; //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:&nbsp; tr.innerHTML =&nbsp; &nbsp; "<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>";&nbsp; &nbsp;&nbsp;&nbsp; tbody.appendChild(tr);}<table id="exp02Table" class="responsive-table">&nbsp; <thead>&nbsp; &nbsp; <tr class="bengaliText">&nbsp; &nbsp; &nbsp; <th> (P)</th>&nbsp; &nbsp; &nbsp; <th> (a cm)</th>&nbsp; &nbsp; &nbsp; <th> (b cm)</th>&nbsp; &nbsp; &nbsp; <th> (c cm)</th>&nbsp; &nbsp; &nbsp; <th> L=(a-b) cm</th>&nbsp; &nbsp; &nbsp; <th> H = (c-b) cm </th>&nbsp; &nbsp; &nbsp; <th> P = H±h</th>&nbsp; &nbsp; &nbsp; <th> PV = (H±h)*L</th>&nbsp; &nbsp; </tr>&nbsp; </thead>&nbsp; <tbody id="exp02TBody">&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; <p> <label> <input value="0" checked name="group1" type="radio" /> <span>বায়ু মন্ডলের চাপ</span> </label> </p>&nbsp; &nbsp; &nbsp; &nbsp; <p> <label> <input value="1" name="group1" type="radio" /> <span>বায়ু মন্ডলের চাপের বেশী</span> </label> </p>&nbsp; &nbsp; &nbsp; &nbsp; <p> <label> <input value="2" name="group1" type="radio" /> <span>বায়ু মন্ডলের চাপের কম</span> </label> </p>&nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; <td contenteditable></td>&nbsp; &nbsp; &nbsp; <td contenteditable></td>&nbsp; &nbsp; &nbsp; <td contenteditable></td>&nbsp; &nbsp; &nbsp; <td contenteditable></td>&nbsp; &nbsp; &nbsp; <td contenteditable></td>&nbsp; &nbsp; &nbsp; <td contenteditable></td>&nbsp; &nbsp; &nbsp; <td contenteditable></td>&nbsp; &nbsp; </tr>&nbsp; </tbody></table><button onclick="AddRow()">Add row</button>我<button onclick="AddRow()">Add row</button>向您的 DOM添加了一个(在 HTML 中),并添加了一个带有该函数的脚本Addrow()(请参阅小提琴以获取参考)。在 jQuery 中:$(document).ready(function() {&nbsp; let i = 1;&nbsp; $("#row-button").on("click", function() {&nbsp; &nbsp; i++;&nbsp; &nbsp; $("#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>");&nbsp; });});<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><table id="exp02Table" class="responsive-table">&nbsp; <thead>&nbsp; &nbsp; <tr class="bengaliText">&nbsp; &nbsp; &nbsp; <th> (P)</th>&nbsp; &nbsp; &nbsp; <th> (a cm)</th>&nbsp; &nbsp; &nbsp; <th> (b cm)</th>&nbsp; &nbsp; &nbsp; <th> (c cm)</th>&nbsp; &nbsp; &nbsp; <th> L=(a-b) cm</th>&nbsp; &nbsp; &nbsp; <th> H = (c-b) cm </th>&nbsp; &nbsp; &nbsp; <th> P = H±h</th>&nbsp; &nbsp; &nbsp; <th> PV = (H±h)*L</th>&nbsp; &nbsp; </tr>&nbsp; </thead>&nbsp; <tbody id="exp02TBody">&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; <p> <label> <input value="0" checked name="group1" type="radio" /> <span>বায়ু মন্ডলের চাপ</span> </label> </p>&nbsp; &nbsp; &nbsp; &nbsp; <p> <label> <input value="1" name="group1" type="radio" /> <span>বায়ু মন্ডলের চাপের বেশী</span> </label> </p>&nbsp; &nbsp; &nbsp; &nbsp; <p> <label> <input value="2" name="group1" type="radio" /> <span>বায়ু মন্ডলের চাপের কম</span> </label> </p>&nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; <td contenteditable></td>&nbsp; &nbsp; &nbsp; <td contenteditable></td>&nbsp; &nbsp; &nbsp; <td contenteditable></td>&nbsp; &nbsp; &nbsp; <td contenteditable></td>&nbsp; &nbsp; &nbsp; <td contenteditable></td>&nbsp; &nbsp; &nbsp; <td contenteditable></td>&nbsp; &nbsp; &nbsp; <td contenteditable></td>&nbsp; &nbsp; </tr>&nbsp; </tbody></table><button id="row-button">Add row</button>

三国纷争

希望这会有所帮助HTML<table id="exp02Table" class="responsive-table table table-bordered">&nbsp; <thead>&nbsp; &nbsp;<tr class="bengaliText">&nbsp; &nbsp; <th> (P)</th>&nbsp; &nbsp; <th> (a cm)</th>&nbsp; &nbsp; <th> (b cm)</th>&nbsp; &nbsp; <th> (c cm)</th>&nbsp; &nbsp; <th> L=(a-b) cm</th>&nbsp; &nbsp; <th> H = (c-b) cm </th>&nbsp; &nbsp; <th> P = H±h</th>&nbsp; &nbsp; <th> PV = (H±h)*L</th>&nbsp; </tr>&nbsp;</thead>&nbsp;<tbody id="exp02TBody">&nbsp; &nbsp;<tr>&nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp;<p> <label> <input value="0" checked name="group1" type="radio" /> <span>বায়ু মন্ডলের চাপ</span> </label> </p>&nbsp; &nbsp; <p> <label> <input value="1" name="group1" type="radio" /> <span>বায়ু মন্ডলের চাপের বেশী</span> </label> </p>&nbsp; &nbsp; <p> <label> <input value="2" name="group1" type="radio" /> <span>বায়ু মন্ডলের চাপের কম</span> </label> </p>&nbsp; </td>&nbsp; <td contenteditable></td>&nbsp; <td contenteditable></td>&nbsp; <td contenteditable></td>&nbsp; <td contenteditable></td>&nbsp; <td contenteditable></td>&nbsp; <td contenteditable></td>&nbsp; <td contenteditable></td>&nbsp; </tr>&nbsp;</tbody></table><button class="addN">&nbsp;Add New</button>JS$(".addN").on("click", function(e){&nbsp;var lastTr = $("#exp02TBody").find("tr:last-child")&nbsp;var clone = lastTr.clone();&nbsp;var groupName&nbsp; = lastTr.find("input[type='radio']").attr("name");&nbsp;var lastGroupNum = parseInt(groupName.match(/\d/g).join(""));&nbsp;var newGroup = "group"+(lastGroupNum+1);&nbsp;clone.find("input[type='radio']").attr("name", newGroup);&nbsp;$("#exp02TBody").append(clone);})
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript