在下面的动态表单中,可以动态添加或删除行。计算涉及找出成本价格与产品销售价格之间的差异。
我喜欢实现(并且在最后一周左右尝试徒劳)是将结果单元格的背景颜色设置为红色(如果值为负),如果值为正则设置为绿色。虽然我在这个网站上遇到了几个解决方案,但遗憾的是我无法实现它们来解决这个问题。
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>CSS</title>
</head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
if (rowCount < 5) { // limit the user from creating fields more than your limits
var row = table.insertRow(rowCount);
//var row = table.insertRow(rowCount-1);
var colCount = table.rows[0].cells.length;
row.id = 'row_'+rowCount;
for (var i = 0; i < colCount; i++) {
var newcell = row.insertCell(i);
newcell.outerHTML = table.rows[0].cells[i].outerHTML;
}
var listitems= row.getElementsByTagName("input")
for (i=0; i<listitems.length; i++) {
listitems[i].setAttribute("oninput", "calculate('"+row.id+"')");
}
} else {
alert("Maximum 5 Rows Only.");
}
}
function deleteRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
for (var i = 0; i < rowCount; i++) {
var row = table.rows[i];
var chkbox = row.cells[0].childNodes[0];
if (null !== chkbox && true === chkbox.checked) {
if (rowCount <= 1) { // limit the user from removing all the fields
alert("Cannot Remove all the Records.");
break;
}
table.deleteRow(i);
rowCount--;
i--;
}
}
}
</script>
<script>
function calculate(elementID) {
var mainRow = document.getElementById(elementID);
var num1 = mainRow.querySelectorAll('[name=num1]')[0].value;
var num2 = mainRow.querySelectorAll('[name=num2]')[0].value;
var result = mainRow.querySelectorAll('[name=result]')[0];
var result1 = num2 - num1;
result.value = result1;
}
</script>
<body>
<input type="button" value="Add" onClick="addRow('dataTable')" />
<input type="button" value="Remove" onClick="deleteRow('dataTable')" />
<table id="dataTable" class="form" border="1">
<tbody>
<tr id='row_0'>
<td><input type="checkbox" required="required" name="chk[]" checked="checked" /></td>
<td>
<label>Cost Price</label>
<input type="text" size="1" required="required" name="num1" class="num1" id="num1" oninput="calculate('row_0')"/>
</td>
<td>
<label>Selling Price</label>
<input type="text" size="1" required="required" name="num2" class="num2" id="num2" oninput="calculate('row_0')"/>
</td>
<td>
<label>Difference</label>
<input type="text" size="1" required="required" name="result" class="result" id="result" />
</td>
</tr>
</tbody>
</table>
</body>
</html>
答案 0 :(得分:1)
除了HTML问题(如您对问题的评论中所述),您可以通过检查result1
变量是否大于零,并将相关样式应用于父td
来实现目标input
的。{试试这个:
function calculate(elementID) {
var mainRow = document.getElementById(elementID);
var num1 = mainRow.querySelectorAll('[name=num1]')[0].value;
var num2 = mainRow.querySelectorAll('[name=num2]')[0].value;
var result = mainRow.querySelectorAll('[name=result]')[0];
var cell = result.parentElement;
var result1 = num2 - num1;
result.value = result1;
var cellBackground = result1 < 0 ? 'red' : 'transparent';
cell.style.backgroundColor = cellBackground;
}
答案 1 :(得分:0)
<h:inputText id="filterName" value="#{bean.name}" >
<f:validator validatorId="simpleNameValidator" />
<f:validator binding="#{customValidator}" disabled="#{facesContext.validationFailed()}"/>
</h:inputText>