![](/img/trans.png)
[英]How do I initiate a javascript:alert box only if a certain word is typed into the input field?
[英]How to reset only the Input on the Alert Box in Javascript?
我正在使用需要從下拉列表中進行選擇的表單構造一個簡單的計算器,該表單在運行時將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>
添加一個功能來清除輸入字段:
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";
}
你是說confirm()? alert()不會返回值,但是confirm()會返回:
if (confirm("Do you want to do something?")) {
//Ok was pressed.
}
else {
//Cancel/close was pressed.
}
注意: 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 = "";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.