簡體   English   中英

JavaScript數據類型條件

[英]Javascript Data Type Condition

今天,我發現了一個條件極少的巨魔。 但是基本是“如果我通過考試,那么我就必須安排一個聚會。但是,如果我考試不及格,那么我就必須專心學習。”

這是一個簡單的if,else條件。 但是我面臨太多問題。 我不得不提到我正在學習JS。 對我來說,這么簡單的問題似乎很難。

我提出了“是否通過”的條件。 但是,當我輸入價值證明“通過條件”時,我感到震驚。

注意:我將“通過”標記為“好”,將“失敗”標記為“差”。

我所面對的讓我解釋一下:(

  1. 當我輸入“好”字除外。 然后我需要輸入“好”或“不好”的信息

  2. 如果我輸入任何數字,那么我需要輸入一個消息來輸入“好”或“壞”

  3. 如果我提交的輸入字段沒有任何值。 然后我需要輸入“好”或“不好”或其他信息

我親手制作了劇本。 但是我面臨太多未解決的問題。

  1. 如果我按照此模式“ textnumber”輸入輸入,例如 “ good123”然后顯示未定義。 我不是為什么:(為什么將“ good123”標記為“ typeof”“ undefined”?

  2. 對我來說最神秘的事情。 如果輸入“良好”,則顯示正確的條件輸出,但是如果輸入“良好”,則顯示未定義。 我的意思是:如果輸入區分大小寫的單詞,則輸入不滿足條件。 在我的示例中,我以某種方式解決了它。 但是請找我,有什么辦法可以解決這個問題。

最后,我用自己的方式編寫了一個腳本。 可以使用其他任何方式來創建此項目,也可以通過其他方式簡化腳本。 如果您建議我,我將不勝感激。 還請建議我如何克服所面臨的問題。 沒有我的方式,可以通過其他任何方式解決這些已注意到的問題。

問候里扎爾

 <!--The Script Is Made By Md Rizaur Rahman from https://stackoverflow.com/users/9192572/md-rizaur-rahman--> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Simple Logic By Md Rizaur Rahman</title> </head> <body> <script> function logic() { var userInput = document.getElementById("logic").value; var input = userInput.toLowerCase(); var onlyStr = /^[a-zA-z]+$/; var onlyNum =/^[0-9]+$/; var output; if(input === "good"){ output = "Lets Party" } else if(input === "bad"){ output = "Please Study Attentively" } else if ((input.match(onlyStr)) && (input !== "good")){ output = "You Enter String Without \\"Good\\" Word. Please Enter Good or Bad"; } else if(input.match(onlyNum)){ output = "You Enter A Number. Please Enter Good or Bad"; } else if (input.trim() == ""){ output = "Please Enter Any Value or Please Enter Good or Bad"; } else{ output = "Your Input Type Is Marked As Undefined. Please Enter Any Value or Please Enter Good or Bad"; } alert (output); } </script> <input type="text" id="logic"> <button onclick="logic()" type="submit" id="input">Submit</button> <p id="html_output"></p> </body> </html> 

它被標記為未定義,因為它不滿足上述任何條件(它不匹配正則表達式,也不是“好”或“壞”)。 將正則表達式更改為/^[a-zA-z0-9]+$/ (接受0-9之間的數字),然后使用!isNaN(input)將檢查數值放在else if 在檢查輸入等於“好”還是“壞”之前,還應該將輸入更改為小寫。 為了使isNaN檢查正常工作,您還需要檢查input是否全部為空格( if(!input.trim().length) ),因為空格也被視為數字(不是NaN)。

 <!--The Script Is Made By Md Rizaur Rahman from https://stackoverflow.com/users/9192572/md-rizaur-rahman--> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Simple Logic By Md Rizaur Rahman</title> </head> <body> <script> function logic() { var userInput = document.getElementById("logic").value; var input = userInput.toLowerCase(); var onlyStr = /^[a-zA-z0-9]+$/; var onlyNum =/^[0-9]+$/; var output; if(input.toLowerCase() === "good"){ output = "Lets Party" } else if(input.toLowerCase() === "bad"){ output = "Please Study Attentively" } else if(!input.trim().length){ output = "Input can not be empty!"; } else if(!isNaN(input)){ output = "You Enter A Number. Please Enter Good or Bad"; } else if ((input.match(onlyStr)) && (input !== "good")){ output = "You Enter String Without \\"Good\\" Word. Please Enter Good or Bad"; } else if (input.trim() == ""){ output = "Please Enter Any Value or Please Enter Good or Bad"; } else{ output = "Your Input Type Is Marked As Undefined. Please Enter Any Value or Please Enter Good or Bad"; } alert (output); } </script> <input type="text" id="logic"> <button onclick="logic()" type="submit" id="input">Submit</button> <p id="html_output"></p> </body> </html> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM