简体   繁体   English

如何仅重置Javascript警报框中的输入?

[英]How to reset only the Input on the Alert Box in Javascript?

I am constructing a simple calculator using a form that requires selections from a drop down which when run inputs 3 different calculations into 3 different fields. 我正在使用需要从下拉列表中进行选择的表单构造一个简单的计算器,该表单在运行时将3个不同的计算输入3个不同的字段。 Anyway -- I have put some js in the code to prevent invalid selection combinations, using an alert to inform the user. 无论如何-我在代码中放置了一些js来防止无效的选择组合,并使用警报通知用户。 What I would like to do is reset only the inputs (changing none of the drop downs when the user clicks "ok" on the alert box. I'm a newbie, tried several things and not come close. Any help? 我想做的是仅重设输入(当用户单击警报框上的“确定”时,不更改任何下拉菜单。我是新手,尝试了几件事却没有成功。有什么帮助吗?

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

Add a function to clear the input fields: 添加一个功能来清除输入字段:

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

and call it along with each alert() call: 并与每个alert()调用一起调用它:

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

(Note: the condition in the if should use the == operator, which is used for comparison. = is for assignments.) (注意: if的条件应使用==运算符,用于比较。 =用于赋值。)

By the way, your else block is missing the curly braces, it should say: 顺便说一句,您的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";
}

Do you mean confirm()? 你是说confirm()? alert() doesn't return a value, but confirm() does: alert()不会返回值,但是confirm()会返回:

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

Note: if (A = "null") should be if (A == "null") same for B and C 注意: if (A = "null")应该与if (A == "null")对于B和C相同

To reset the textboxes, you can do 要重置文本框,您可以执行

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

where clearTextBoxes() would be something like 其中clearTextBoxes()类似于

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM