如果所有三个文本框都出现错误,则Javascript验证文本框<&燃气轮机;那么100%
我有一个表格,其中包含4个文本框,我需要验证帮助 我需要总组合值=100%,如果小于或大于,则显示错误 e、 g 框1用户输入50%,框2用户输入30%,框3用户输入19%。 方框4将包含方框1、2和3的总价值,即总计99% 当用户单击提交时,它应该验证并返回错误,例如,总计不等于100% 如果方框4的总数大于100%,则情况也是如此 目前,我已经计算了框4的总值,但不确定如何进行验证如果所有三个文本框都出现错误,则Javascript验证文本框<&燃气轮机;那么100%,javascript,html,validation,textbox,Javascript,Html,Validation,Textbox,我有一个表格,其中包含4个文本框,我需要验证帮助 我需要总组合值=100%,如果小于或大于,则显示错误 e、 g 框1用户输入50%,框2用户输入30%,框3用户输入19%。 方框4将包含方框1、2和3的总价值,即总计99% 当用户单击提交时,它应该验证并返回错误,例如,总计不等于100% 如果方框4的总数大于100%,则情况也是如此 目前,我已经计算了框4的总值,但不确定如何进行验证 <SCRIPT language="JavaScript"> function Calc
<SCRIPT language="JavaScript">
function CalculateCardT()
{
formSectionA.CardTotal.value =
parseFloat(formSectionA.strPercDeferredGoods.value) + parseFloat(formSectionA.strPercDeposits.value) + parseFloat(formSectionA.strPercSubscriptions.value);
}
</SCRIPT>
<tr>
<td rowspan="2" nowrap="nowrap"> % of CARD turnover</td>
<td nowrap="nowrap">% Deferred delivery of goods</td>
<td nowrap="nowrap">% Deposit is taken</td>
<td nowrap="nowrap">% Subscriptions</td>
<td nowrap="nowrap">% Total</td>
</tr>
<tr>
<td nowrap="nowrap">
<input name="strPercDeferredGoods" type="text" required="required" id="strPercDeferredGoods" tabindex="6" onBlur="CalculateCardT();" onChange="CalculateCardT();" size="3" maxlength="3" /></td>
<td nowrap="nowrap">
<input name="strPercDeposits" type="text" required="required" id="strPercDeposits" " tabindex="7" onBlur="CalculateCardT();" onChange="CalculateCardT();" size="3" maxlength="3" />
</td>
<td nowrap="nowrap">
<input name="strPercSubscriptions" type="text" required="required" id="strPercSubscriptions" tabindex="8" onblur="CalculateCardT();" onChange="CalculateCardT();" size="3" maxlength="3"/></td>
<td nowrap="nowrap">
<input name="CardTotal" type="text" id="CardTotal" style="background-color:#CCC" size="3" maxlength="3" onKeyDown="return false;"/></td>
</tr>
函数CalculateCardT()
{
formSectionA.CardTotal.value=
parseFloat(formSectionA.strPercDeferredGoods.value)+parseFloat(formSectionA.strPercDeposits.value)+parseFloat(formSectionA.strPercSubscriptions.value);
}
%信用卡周转率
%延期交货
%押金被接受了
%订阅
%总数
首先,您使用的代码已经过时,并且存在语法错误
下面是语法正确的HTML的简短版本
<table>
<thead>
<tr>
<th>% Deferred delivery of goods</th>
<th>% Deposit is taken</th>
<th>% Subscriptions</th>
<th>% Total</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="text" id="strPercDeferredGoods" />
</td>
<td>
<input type="text" id="strPercDeposits" />
</td>
<td>
<input type="text" id="strPercSubscriptions" />
</td>
<td>
<input type="text" id="CardTotal" disabled="disabled" />
</td>
</tr>
</tbody>
</table>
%延期交货
%押金被接受了
%订阅
%总数
您尝试过什么吗?使用Dreamweaver是您的第一个错误。您说“代码要轻松”,但您没有在问题中发布任何内容??抱歉。,。这样一个noob,包括我到目前为止所拥有的。哇,我已经很久没有看到
了,谢谢Howard。我会尽快查出来的。谢谢你把时间花在评论上。。。是的,我需要学习。你会推荐什么来代替Dreamweaver?效果很好。谢谢@raymantle—我强烈推荐它作为IDE。快速版是完全免费的,有很多功能可以帮助你学习。嗨,Howard,我还有一个要求-不确定你是否能帮上忙,我需要如果用户在strPercDelferredGoods文本框中输入的值小于100%,然后,它会显示另外3个文本框供用户填写?@raymontle-您应该将此作为一个新问题发布,这样我们就不会因为各种编辑而将原始帖子弄乱。一旦你发布了问题,把链接发给我,我会调查的。谢谢
// IIFE - create local "document"
(function (document) {
// Store all of the "input" elements in the variable "elems"
var elems = document.getElementsByTagName("input");
// Store an array of id's of the items to sum in the variable "idsToSum"
var idsToSum = ["strPercDeferredGoods", "strPercDeposits", "strPercSubscriptions"]
// Loop through the "input" elements
for (var i = 0; i < elems.length; i++) {
// For each "input" element, add a "change" event listener
// When the event happens, run the function
elems[i].addEventListener("change", function () {
// Define a variable "sum" and assign a value of 0 to it
var sum = 0;
// Loop through all of the "idsToSum"
for (var a = 0; a < idsToSum.length; a++) {
// If the value of the current input is a number (removing the % sign if applicable), then add the value to the "sum" variable. Otherwise, add 0
sum += isNaN(parseFloat(document.getElementById(idsToSum[a]).value.replace("%", ""))) ? 0 : parseFloat(document.getElementById(idsToSum[a]).value.replace("%", ""));
}
// If the value of "sum" is greater than 100
if (sum > 100) {
// Clear the "CardTotal" value
document.getElementById("CardTotal").value = "";
// Alert an error
alert("Total is not equal to 100%");
}
// Otherwise
else {
// Assign the value of "CardTotal" to the value of "sum" and add a % sign
document.getElementById("CardTotal").value = sum + "%";
}
});
}
// IIFE - pass in global "document"
})(document);