html表单必需属性字段

时间:2015-06-14 07:28:12

标签: html required

我的表单未遵守“评论”字段的required属性。我想要它,以便当用户提交表单时,它显示“请填写字段”,但用户可以立即提交表单。

<form class="form-part" method="post" action="contact_form.php" name="contactform" id="contactform" onSubmit="return checkForm1();">
    <input id="name" type="text" name="name" size="30" title="Name" required>
    <input id="email" type="text" name="email" size="30" title="Email" required>
    <textarea rows="3" id="comments" name="comments" cols="40" title="Tell us what you think!" required></textarea>   
    <input type="submit"  name="Submit" alt="Send">
</form>

2 个答案:

答案 0 :(得分:0)

一个非常简单的检查功能可能是:

function checkForm1(frm){
  if (frm.comments.value==''){
    alert('Please leave a comment.' );
    return false;
  else return true;
}

为此,您需要将表单标记扩展为:

<form ... onsubmitt="return checkForm1(this)">

答案 1 :(得分:-1)

在您的代码中从标记中删除

<form class="form-part" method="post" action="contact_form.php"  name="contactform" id="contactform">
<input id="email" type="text" name="email" size="30" title="Email" required>

<textarea rows="3" id="comments" name="comments" cols="40" title="Tell us what you think!" required></textarea>

  <input type="submit"  name="Submit" alt="Send" onSubmit="return checkForm1();">
</form>

现在运行。并告诉我表格是否显示。