【问题标题】:How to reset only the Input on the Alert Box in Javascript?如何在Javascript中仅重置警报框上的输入?
【发布时间】:2011-09-12 21:03:34
【问题描述】:

我正在使用一个表单构建一个简单的计算器,该表单需要从下拉列表中进行选择,当运行时将 3 个不同的计算输入到 3 个不同的字段中。无论如何——我在代码中放了一些 js 来防止无效的选择组合,使用警报来通知用户。我想做的是仅重置输入(当用户单击警报框上的“确定”时,不更改任何下拉菜单。我是新手,尝试了几件事但没有接近。有帮助吗?

<script type="text/javascript">
function clearInputs() {
    document.getElementById("RawCapacity").value = "";
    document.getElementById("RAIDCapacity").value = "";
    document.getElementById("WindowsCapacity").value = "";
    }

function RAIDcalc() {
    //Values put into the calculator
    var A = document.forms[0].DriveCapacity.value *1;
    var B = document.forms[0].NumberOfDisks.value *1;
    var C = document.forms[0].RAID.value *1;
    var D = document.forms[0].HotSpare.value *1;

    //Pre-math for the calculations 
    var E = C + D;
    var F = B - E;
    var G = A * 0.95;

    var H = document.getElementById("RAID").options[document.getElementById("RAID").selectedIndex].text

    //Validate form (to ensure selections are made), 
    //validate values (to prevent impossible combinations), 
    //and to calculate and write the responses
    if (A == "null") {
        alert("Please make a selection in the required fields.");
        clearInputs();
        }
    else if (B == "null") {
        alert("Please make a selection in the required fields.");
        clearInputs();
        }
    else if (C == "null") {
        alert("Please make a selection in the required fields.");
        clearInputs();
        }
    else if (H == "RAID 5" && B<=(D + (1 * 1))) {
        alert("This configuration has too many Hot Spares.");
        }
    else if (H == "RAID 6" && B<=(D + (2 * 1))) {
        alert("This configuration has too many Hot Spares.");

    else if (H == "RAID 6" && B<4) {
        alert("RAID 6 requires a minimum of FOUR disks.");
        }
    else {
        document.forms[0].RawCapacity.value = Math.round(((A * B) / 1000)*Math.pow(10,2))/Math.pow(10,2) + " TB";   
        document.forms[0].RAIDCapacity.value = Math.round(((F * A) / 1000)*Math.pow(10,2))/Math.pow(10,2) + " TB";  
        document.forms[0].WindowsCapacity.value = Math.round(((F * G) / 1000)*Math.pow(10,2))/Math.pow(10,2) + " TB";
        }
    //For testing
    //alert("A="+A+" B="+B+" C="+C+" D="+D+" E="+E+" F="+F+" G="+G);
    //var RAIDtext = document.getElementById("RAID");
    //var RAIDselindex = document.getElementById("RAID").selectedIndex;
    //alert (document.getElementById("RAID").options[document.getElementById("RAID").selectedIndex].text);
    //if (H == "RAID 6" && B<4) alert("H = RAID 6 & B<4!");

    }
</script>

【问题讨论】:

  • 注意:if (A = "null") 应该是 if (A == "null") B 和 C 相同
  • alert(如 confirmprompt 等)是同步的。用户要么点击“确定”,要么离开页面。因此,您只需在调用后直接添加代码即可。
  • 重新发布的代码 -- 现在坏了???

标签: javascript


【解决方案1】:

添加清除输入字段的功能:

function clearInputs() {
    document.getElementById("RawCapacity").value = "";
    document.getElementById("RAIDCapacity").value = "";
    // The same for other fields
}

并与每个alert() 调用一起调用它:

if (A == "null") {
    alert("Please make a selection in the required fields.");
    clearInputs();
}

(注意:if中的条件要使用==运算符,用于比较,=用于赋值。)

顺便说一句,你的 else 块缺少花括号,它应该说:

else {
    document.forms[0].RawCapacity.value = Math.round(((A * B) / 1000)*Math.pow(10,2))/Math.pow(10,2) + " TB";   
    document.forms[0].RAIDCapacity.value = Math.round(((F * A) / 1000)*Math.pow(10,2))/Math.pow(10,2) + " TB";  
    document.forms[0].WindowsCapacity.value = Math.round(((F * G) / 1000)*Math.pow(10,2))/Math.pow(10,2) + " TB";
}

【讨论】:

  • 嗯,我想我破坏了一些试图暗示你的解决方案的东西:
  • else if (H == "RAID 6" &amp;&amp; B&lt;4) { 行之前缺少右大括号。
  • 顺便说一句,您的浏览器会捕捉到这样的语法错误——如果有什么不工作的话,请务必先检查 JavaScript 控制台是否有错误消息。
  • 现在很好用。我在 Web Developer 上尝试了 js 控制台,但我太菜鸟了,对我来说没有多大意义。非常感谢您的帮助。
【解决方案2】:

你的意思是确认()? alert() 不返回值,但 confirm() 会:

if (confirm("Do you want to do something?")) {
  //Ok was pressed.
}
else {
  //Cancel/close was pressed.
}

【讨论】:

  • 我对提供选项并不感兴趣。我只想在警报关闭时操作表单的输入文本字段,而不是表单的其他部分。
【解决方案3】:

注意:if (A = "null") 应该是 if (A == "null") B 和 C 相同

要重置文本框,您可以这样做

 if (A == "null") 
 {
        alert("Please make a selection in the required fields.");
        clearTextBoxes();
 }

clearTextBoxes() 类似于

function clearTextBoxes()
{
     document.forms[0].RawCapacity.value = "";
     document.forms[0].RAIDCapacity.value = "";
     document.forms[0].WindowsCapacity.value = "";

}

【讨论】:

  • -1 您在代码示例中仍然使用了错误的运算符,以及未定义的函数
猜你喜欢
  • 2012-06-11
  • 2016-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-02
  • 2013-01-06
相关资源
最近更新 更多