使用单选按钮和标签组合修剪表格单元格的HTML标签
Trim HTML tags of a table cell with radio button and label combined
Javascript函数:
我下面发布的JS函数是循环遍历在第二个参数中定义的列索引,同时循环也在第三个参数中定义的给定元素id。这将根据所单击行的列值填充文本框、单选按钮和下拉字段。
从下面的示例使用中,当单击表时,来自列索引1 (price)的值将显示在price文本框中。
示例用法:<table id="tblPrice" onclick="gettabledata('tblPrice', '0,1,2,3', 'PriceId,Price,strtDt,endDt')">
问题:
表的第二列(第一列是隐藏的)有一个单选按钮和标签的组合,当点击这一行时,Price文本框的值如下所示:
<input type="radio" name="rbDefaultPrice" class="radio" value=""> <label style="font-size: 1em" class="price" for="lblPriceRbtn">5</label>
我只需要值5
显示在Price文本框中,所以我需要减少显示的元素标签。所有其他只有文本(没有单选按钮)的单元格都被正确复制到相应的文本框中,除了具有单选按钮和标签组合的单元格。负责此操作的代码是:
document.getElementById(fieldId[i]).value = rowSelected.cells[colIndex[i]].innerHTML.trim();
innerHTML.trim()
仅在我获得单元格值时删除空白。
JavaScript(转到带注释的行:Problem
查看有问题的代码):
function gettabledata(tableId, colIndexes, fieldIds) {
var table = document.getElementById(tableId); //Get table id to evaluate
var cells = table.getElementsByTagName('td'); //Get all table cells per column
//Get field Ids set in parameter
var fieldId = fieldIds.split(",");
var colIndex = colIndexes.split(",");
for (var i = 0; i < cells.length; i++) {
var cell = cells[i];
//Get column data on clicked row
cell.onclick = function () {
var rowId = this.parentNode.rowIndex;
var rowsNotSelected = table.getElementsByTagName('tr');
var rowSelected = table.getElementsByTagName('tr')[rowId]; //Get selected row
//Get number of items in array and loop (NOTE: colIndex is an array parameter defined in called javascript from View)
for (i = 0; i < fieldId.length; i++) {
var elmntTag = document.getElementById(fieldId[i]).tagName; //Get element tag name of each filedId item (SELECT, INPUT)
var elmntType = document.getElementById(fieldId[i]).getAttribute("type"); //Get element type of each filedId item (text, radio)
//Check field if dropdown or input (text or radio)
if (elmntTag == "INPUT") {
//Populate field depending on input type
if (elmntType == "text" || elmntType == "hidden") {
//PROBLEM
document.getElementById(fieldId[i]).value = rowSelected.cells[colIndex[i]].innerHTML.trim(); //Populate value of textbox id defined in the parameter
} else if (elmntType == "radio") {
//Populate value of radiobutton id defined in the parameter
var status = rowSelected.cells[colIndex[i]].innerHTML.trim();
var name = document.getElementById(fieldId[i]).getAttribute("name");
if (status == "Active") {
$('input[name=' + name + '][value="Active"]').prop('checked', true);
} else if (status == "Inactive") {
$('input[name=' + name + '][value="Inactive"]').prop('checked', true);
}
}
//Check field if dropdown or input (text or radio)
} else if (elmntTag == "SELECT") {
var dropdown = document.getElementById(fieldId[i]);
dropdown.value = rowSelected.cells[colIndex[i]].innerHTML.trim();
}
}
}
}
$('#btnUpdate').show();
$('#btnSave').hide();
}
HTML: <table id="template" style="display:none;">
<tr>
<td class="hidden"></td>
<td>
<input type="radio" name="rbDefaultPrice" class="radio" />
<label style="font-size: 1em" class="price"></label>
</td>
<td class="startdate"></td>
<td class="enddate"></td>
</tr>
</table>
在查看了几个论坛后,我得出了一个答案:
我将我的代码替换为:
document.getElementById(fieldId[i]).value = rowSelected.cells[colIndex[i]].innerHTML.trim();
:
document.getElementById(fieldId[i]).value = rowSelected.cells[colIndex[i]].textContent.trim();
- 在<页眉>标签
- Ckeditor-plugin:插入虚假元素add不情愿<p>标签前后
- 我应该使用Ng提交还是点击表格
- CSS表格:从列平移到整个表格宽度
- 将HTML表格导出到excel时,无法将数据加载到excel文件中
- CSS-若窗口太小,滚动条会出现在“表格”单元格上
- iframe正在添加标签,需要删除它们
- 设置“;将标签视为文本“;通过脚本将Google电子表格图表的值设置为true
- 将单个 id 标签添加到表格单元格中,每个循环都有 Rails
- 仅在一个表格行内设置单选按钮标签的样式
- 如何将文本从 Google 电子表格中的多个单元格提取到网站,每个单元格都位于不同的
标签中
- 我想提交一个表格(没有<表格>标签),当我按键盘上的回车键时
- HTML, CSS, JS -在表格显示中将文本区域与标签对齐
- 表格单元格中预填充的文本显示为“未定义”.当在JS中添加HTML标签时
- 使用单选按钮和标签组合修剪表格单元格的HTML标签
- JQuery或Javascript获得标签文本在表格单元格与单选按钮和标签的组合
- 通过getContext - Google Apps Script -电子表格从HTML标签中获取文本
- 在带有无线电的html表格中显示带有相关标签的文本框
- 表格td标签中弹出窗口提示的问题
- 如何删除表格中每个td标签的第一个字符,然后用Google Chart API和Javascript显示它