Html 当有需要填写的文本字段时,如何制作弹出提醒?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22399640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:14:31  来源:igfitidea点击:

How to make a pop-up alert when there are text fields that are needed to be filled up?

htmlpopupalert

提问by user3418987

I have a form that adds customer information. I would just like to know how to make a pop up alert box that warns the user that he/she has forgotten to fill up an important text field. I am not good in HTML or JavaScript so I need help on this.

我有一个添加客户信息的表单。我只想知道如何制作一个弹出警告框,警告用户他/她忘记填写重要的文本字段。我不擅长 HTML 或 JavaScript,所以我需要这方面的帮助。

回答by mahesh

You can use the below example

您可以使用以下示例

<html>
<head>
<script>
function validateForm() {
    var x = document.forms["myForm"]["fname"].value;
    if (x == null || x == "") {
        alert("Name must be filled out");
        return false;
    }
}
</script>
</head>
<body>

<form name="myForm" action="js"
onsubmit="return validateForm()" method="get">
Name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>

</body>
</html>

回答by Ephenodrom

Simple as hell :)

简单到地狱:)

Just use the parameter "required" at your input field ( textfield / textarea etc. ).

只需在输入字段(文本字段/文本区域等)中使用参数“必需”。

<form method="post" name="Form" onsubmit="" action="">
    <input length="20" required=""></input>
    <input type="submit" value="Submit"></input>
</form>

A little text will show up if the textinput is empty when the user tries to submit the form! The form can only be submitted if every input that is marked as required is filled with text. Check out this simple fiddle :

当用户尝试提交表单时,如果 textinput 为空,则会显示一些文本!仅当标记为必需的每个输入都填充有文本时,才能提交表单。看看这个简单的小提琴:

http://jsfiddle.net/UL5pC/

http://jsfiddle.net/UL5pC/

回答by Shlomo

Check this code example from this answer:

这个答案检查这个代码示例:

Replace the input fields with your fields of course and set up your own message if any of the fields is missing.

当然,用您的字段替换输入字段,并在缺少任何字段时设置您自己的消息。

<script type="text/javascript">
    function validateForm()
    {
    var a=document.forms["Form"]["ans_a"].value;
    var b=document.forms["Form"]["ans_b"].value;
    var c=document.forms["Form"]["ans_c"].value;
    var d=document.forms["Form"]["ans_d"].value;
    if (a==null || a=="",b==null || b=="",c==null || c=="",d==null || d=="")
      {
      alert("Please Fill All Required Field");
      return false;
      }
    }
    </script>

<form method="post" name="Form" onsubmit="return validate()" action="">
<textarea cols="30" rows="2" name="ans_a" id="a">
<textarea cols="30" rows="2" name="ans_b" id="b">
<textarea cols="30" rows="2" name="ans_c" id="c">
<textarea cols="30" rows="2" name="ans_d" id="d"></textarea>
</form>