0

我在 HTML 中创建了一个表单和调查,用户将在其中填写信息并选择下拉菜单项作为问题的答案。

问题是在没有选择所需的下拉菜单后,系统会提示用户该字段是必需的。如果他们现在选择该下拉菜单并点击提交,则会打印另一个所需的帖子,并且表格不会发送到感谢页面。如果您继续点击提交,它只会一个接一个地发布必填字段。下面是我服务器上表格的链接。

表格链接:http: //amckeedesignportfolio.com/eLearningModule/newForm.php

这是我的 HTML

<h3>
  1. How often do you have a patient who resists oral care? 
  <span style="padding-left:25px;"></span>
</h3>
<br/>
<select name="question1" class="surveySelection1_class">
  <option value=""selected="selected">--select--</option>
  <option value="Never">Never</option>
  <option value="Rarely">Rarely</option>
  <option value="Sometimes">Sometimes</option>
  <option value="Always">Always</option>
</select>

<h3>
  2. How often do you have a patient bite on oral swabs with oral care? 
  <span style="padding-left:25px;"></span>
</h3>
<select name="question2" class="surveySelection2_class">
  <option value=""selected="selected">--select--</option>
  <option value="Never">Never</option>
  <option value="Rarely">Rarely</option>
  <option value="Sometimes">Sometimes</option>
  <option value="Always">Always</option>
</select>

这是 JAVASCRIPT

<script type="text/javascript">
$(document).ready(function(){
  // validate signup form on keyup and submit
  $("#contactForm").validate({
    rules: {
      fName: "required",
      lName: "required",
      telephone: "required",
      email: {
        required: true,
        email: true
      },
      telephone: {
        required: true,
        phoneUS: true
      },
      position      : "required",
      hospital      : "required",
      hospitalCity  : "required",
      hospitalState : "required",
      floor         : "required",
      experience    : "required",
      question1     : "required",
      question2     : "required",
      question3     : "required",
      question4     : "required",
      question5     : "required",
      question6     : "required",
      question7     : "required",
      question8     : "required",
      question9     : "required",
      question10    : "required",
      question11    : "required",
    },

    messages: {
      fName        : "Please enter your firstname.",
      lName        : "Please enter your lastname.",
      telephone    : "Please enter a valid telephone number.",
      email        : "Please specify a valid email address.",
      position     : "Please enter your current position.",
      hospitalCity : "Please enter your current hospital.",
      floor        : "Please enter the current floor or area you work.",
    }
  });

  /* state validation*/
  $validator.addMethod(
    "state_class",
    function(value, element) {
      return this.optional(element) || ( value.indexOf("") == -1);
    },
    "Please select a state."); 

  /* experience validation*/
  $validator.addMethod(
    "selection_class",
    function(value, element) {
      return this.optional(element) || ( value.indexOf("") == -1);
    },
    "Please share your experience with us."); 

  /* question1 validation*/
  $validator.addMethod(
    "surveySelection1_class",
    function(value, element) {
      return this.optional(element) || ( value.indexOf("") == -1);
    },
    "Please choose an answer."); 

  /* question2 validation*/
  $validator.addMethod(
    "surveySelection2_class",
    function(value, element) {
      return this.optional(element) || ( value.indexOf("") == -1);
    },
    "Please choose an answer.");

  /* question3 validation*/
  $validator.addMethod(
    "surveySelection3_class",
    function(value, element) {
      return this.optional(element) || ( value.indexOf("") == -1);
    },
    "Please choose an answer.");

  /* question4 validation*/
  $validator.addMethod(
    "surveySelection4_class",
    function(value, element) {
      return this.optional(element) || ( value.indexOf("") == -1);
    },
    "Please choose an answer.");

  /* question5 validation*/
  $validator.addMethod(
    "surveySelection5_class",
    function(value, element) {
      return this.optional(element) || ( value.indexOf("") == -1);
    },
    "Please choose an answer.");

  /* question6 validation*/
  $validator.addMethod(
    "surveySelection6_class",
    function(value, element) {
      return this.optional(element) || ( value.indexOf("") == -1);
    },
    "Please choose an answer.");

  /* question7 validation*/
  $validator.addMethod(
    "surveySelection7_class",
    function(value, element) {
      return this.optional(element) || ( value.indexOf("") == -1);
    },
    "Please choose an answer.");

  /* question8 validation*/
  $validator.addMethod(
    "surveySelection8_class",
    function(value, element) {
      return this.optional(element) || ( value.indexOf("") == -1);
    },
    "Please choose an answer.");

  /* question9 validation*/
  $validator.addMethod(
    "surveySelection9_class",
    function(value, element) {
      return this.optional(element) || ( value.indexOf("") == -1);
    },
    "Please choose an answer.");

  /* question10 validation*/
  $validator.addMethod(
    "surveySelection10_class",
    function(value, element) {
      return this.optional(element) || ( value.indexOf("") == -1);
    },
    "Please choose an answer.");

  /* question11 validation*/
  $validator.addMethod(
    "surveySelection11_class",
    function(value, element) {
      return this.optional(element) || ( value.indexOf("") == -1);
    },
    "Please choose an answer.");
}); 
</script>
4

1 回答 1

-1

看来您已经编写了很多代码来验证您的表单...使用 jquery-validation 插件验证选择非常简单。您必须确保有一个带有空值的选项。然后您只需将“必需”类添加到选择中。

<select id="mySelect" class="required">
  <option value="">--select--</option>
  <option value="value1">option 1</option>
  <option value="value2">option 2</option>
  <option value="value3">option 3</option>
</select>

请参阅示例:选择验证示例

尝试像这样进行验证,它应该可以解决您的问题。

于 2012-05-12T15:58:36.670 回答