English 中文(简体)
Javacast to Validate 表格
原标题:JavaScript to Validate Form if certain selections are made and show dialog box
  • 时间:2023-09-20 01:29:57
  •  标签:
  • javascript

我的超文本形式有3个部分是,用户没有问题。 现有文字确保只选择对三个部分中的一部分的答复。

我现在要补充一点,即用户在任何部分选择不作所有答复时,应当作进一步的验证。 如果他们不选择那部分问题的话,我要向2个县(K和Cancel)和“你没有选择回答”的案文投掷方言。 你们是否希望继续?”

我的发言稿和现在的发言稿如下:

$(document).ready(function() {
  $("#editRecord").on("submit", function(e) {
    var buttonClicked = $("input[type=submit][clicked=true]").val();

    if (buttonClicked === "Continue") {
      if (!validateFinish(e)) {
        e.preventDefault();
        console.log("Continue");
      }
    } else if (buttonClicked === "Save Draft") {
      if (!validateSaveDraft()) {
        e.preventDefault();
        console.log("Save Draft");
      }
    }
  });

  $("input[type=submit]").click(function() {
    $("input[type=submit]", $(this).parents("form")).removeAttr("clicked");
    $(this).attr("clicked", "true");
  });

  function validateFinish(ev) {
    // Add your validation logic for the Finish button here
    //alert("Form Validation is starting!");

    var sectionsTouched = $( tbody ).has( :checked ),
      inputs = {},
      errorMessage =   ;

    if (sectionsTouched.length === 0) {
      errorMessage =  Please check something before submitting. ;
    } else if (sectionsTouched.length > 1) {
      errorMessage =  Please complete A or B or C only ;
    } else {
      sectionsTouched.find( input ).each(function(i, e) {
        var me = $(e),
          name = me.attr( name );
        inputs[name] = !!inputs[name] || me.is( :checked );
      });
      $.each(inputs, function(k, v) {
        if (!v) {
          errorMessage =  It appears you have not completed all questions. ;
        }
        return v;
      });
    }
    if (errorMessage !==   ) {
      $( #error ).html(errorMessage);
      ev.preventDefault();
      return false;
    }
    return true;
  }

  function validateSaveDraft() {
    // Add your validation logic for the Save Draft button here
    //alert("Just saving a Draft!");
    return true;
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<form action="rateArticleCodes.php" method="post" class="" id="editRecord">
  <input type="hidden" name="recid" value="123445">


  <div class="rate-buttons left py2">
    <input type="submit" name="buttonType" id="saveDraft" value="Save Draft" class="mr1">
    <input type="reset" name="buttonType" value="Reset" class="mx1">
    <input type="submit" name="buttonType" id="submit" value="Continue" class="ml1">
  </div>
  <div id="error" style="color: red"></div>

  <table>
    <thead>
      <tr>
        <th class="nowrap text-left" colspan="2">Section 1</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>A1.</td>
        <td class="nowrap form-radio"><label>
                                                        <input name="CriteriaA1" type="radio" value="Yes"><span>Yes</span></label><label class="ml3"><input name="CriteriaA1" type="radio" value="No"><span>No</span></label></td>
      </tr>
      <tr>
        <td>A2.</td>
        <td class="nowrap form-radio"><label>
                                                        <input name="CriteriaA2" type="radio" value="Yes"><span>Yes</span></label><label class="ml3"><input name="CriteriaA2" type="radio" value="No"><span>No</span></label></td>
      </tr>
      <tr>
        <td>A3.</td>
        <td class="nowrap form-radio"><label>
                                                        <input name="CriteriaA3" type="radio" value="Yes"><span>Yes</span></label><label class="ml3"><input name="CriteriaA3" type="radio" value="No"><span>No</span></label></td>
      </tr>
      <tr>
        <td>A4.</td>
        <td class="nowrap form-radio"><label>
                                                        <input name="CriteriaA4" type="radio" value="Yes"><span>Yes</span></label><label class="ml3"><input name="CriteriaA4" type="radio" value="No"><span>No</span></label></td>
      </tr>
      <tr>
        <td>A5.</td>
        <td class="nowrap form-radio"><label>
                                                        <input name="CriteriaA5" type="radio" value="Yes"><span>Yes</span></label><label class="ml3"><input name="CriteriaA5" type="radio" value="No"><span>No</span></label></td>
      </tr>
    </tbody>
  </table>

  <p class="m2"><strong>OR</strong></p>

  <table>
    <thead>
      <tr>
        <th class="nowrap text-left" colspan="2">Section 2</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>B1. </td>
        <td class="nowrap form-radio"><label>
                                                        <input name="CriteriaB1" type="radio" value="Yes"><span>Yes</span></label><label class="ml3"><input name="CriteriaB1" type="radio" value="No"><span>No</span></label></td>
      </tr>
      <tr>
        <td>B2.</td>
        <td class="nowrap form-radio"><label>
                                                        <input name="CriteriaB2" type="radio" value="Yes"><span>Yes</span></label><label class="ml3"><input name="CriteriaB2" type="radio" value="No"><span>No</span></label></td>
      </tr>
      <tr>
        <td>B3.</td>
        <td class="nowrap form-radio"><label>
                                                        <input name="CriteriaB3" type="radio" value="Yes"><span>Yes</span></label><label class="ml3"><input name="CriteriaB3" type="radio" value="No"><span>No</span></label></td>
      </tr>
    </tbody>
  </table>

  <p class="m2"><strong>OR</strong></p>

  <table>
    <thead>
      <tr>
        <th class="nowrap text-left" colspan="2">Section 3</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>C1.</td>
        <td class="nowrap form-radio"><label>
                                                        <input name="CriteriaC1" type="radio" value="Yes"><span>Yes</span></label><label class="ml3"><input name="CriteriaC1" type="radio" value="No"><span>No</span></label></td>
      </tr>
      <tr>
        <td>C2.</td>
        <td class="nowrap form-radio"><label>
                                                        <input name="CriteriaC2" type="radio" value="Yes"><span>Yes</span></label><label class="ml3"><input name="CriteriaC2" type="radio" value="No"><span>No</span></label></td>
      </tr>
      <tr>
        <td>C3. </td>
        <td class="nowrap form-radio"><label>
                                                        <input name="CriteriaC3" type="radio" value="Yes"><span>Yes</span></label><label class="ml3"><input name="CriteriaC3" type="radio" value="No"><span>No</span></label></td>
      </tr>
      <tr>
        <td>C4. </td>
        <td class="nowrap form-radio"><label>
                                                        <input name="CriteriaC4" type="radio" value="Yes"><span>Yes</span></label><label class="ml3"><input name="CriteriaC4" type="radio" value="No"><span>No</span></label></td>
      </tr>
      <tr>
        <td>C5. </td>
        <td class="nowrap form-radio"><label>
                                                        <input name="CriteriaC5" type="radio" value="Yes"><span>Yes</span></label><label class="ml3"><input name="CriteriaC5" type="radio" value="No"><span>No</span></label></td>
      </tr>
      <tr>
        <td>C6. </td>
        <td class="nowrap form-radio"><label>
                                                <input name="CriteriaC6" type="radio" value="Yes"><span>Yes</span></label><label class="ml3"><input name="CriteriaC6" type="radio" value="No"><span>No</span></label></td>
      </tr>
    </tbody>
  </table>


  <div class="rate-buttons left py2">
    <input type="reset" name="buttonType" value="Reset" class="mx1">
    <input type="submit" name="buttonType" id="submit" value="Continue" class="ml1">
  </div>
  <div id="error" style="color: red"></div>

  <!-- to here -->

</form>

我不肯定,如果继续纽顿被点击,那么如何检查“无”答案,然后如果是,就会发现一个方言箱?

问题回答

你可以核实每个领域的价值,以核实其是否具有“Yes或“No”。

inputs[name] = $("input[name= "+name+" ]:checked").val() || false;

Then simply create an array with the values and finally check if all values are "No" using every function. Below is a working demo.

 $(document).ready(function () {
      $("#editRecord").on("submit", function (e) {
        var buttonClicked = $("input[type=submit][clicked=true]").val();

        if (buttonClicked === "Continue") {
          if (!validateFinish(e)) {
            e.preventDefault();
            console.log("Continue");
          }
        } else if (buttonClicked === "Save Draft") {
          if (!validateSaveDraft()) {
            e.preventDefault();
            console.log("Save Draft");
          }
        }
      });

      $("input[type=submit]").click(function () {
        $("input[type=submit]", $(this).parents("form")).removeAttr("clicked");
        $(this).attr("clicked", "true");
      });

      function validateFinish(ev) {
        // Add your validation logic for the Finish button here
        //alert("Form Validation is starting!");

        var sectionsTouched = $("tbody").has(":checked"),
          inputs = {},
          errorMessage = "";

        if (sectionsTouched.length === 0) {
          errorMessage = "Please check something before submitting.";
        } else if (sectionsTouched.length > 1) {
          errorMessage = "Please complete A or B or C only";
        } else {
          sectionsTouched.find("input").each(function (i, e) {
            var me = $(e),
              name = me.attr("name");
            inputs[name] = $("input[name= "+name+" ]:checked").val() || false;
          });

          $.each(inputs, function (k, v) {
            if (!v) {
              errorMessage = "It appears you have not completed all questions.";
            }
            return v;
          });

          var isAllNo = Object.values(inputs).every(function(q){
              return q ===  No 
          });

          if(isAllNo){
            if (confirm( You have selected "No" for your answers. Do you wish to continue? )) {
              console.log( Thing was saved to the database. );
            } else {
              console.log( Thing was not saved to the database. );
              ev.preventDefault();
            }          
          }
        }
        if (errorMessage !== "") {
          $("#error").html(errorMessage);
          ev.preventDefault();
          return false;
        }
        return true;
      }

      function validateSaveDraft() {
        // Add your validation logic for the Save Draft button here
        //alert("Just saving a Draft!");
        return true;
      }
    });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
  <form action="rateArticleCodes.php" method="post" class="" id="editRecord">
    <input type="hidden" name="recid" value="123445" />

    <div class="rate-buttons left py2">
      <input
        type="submit"
        name="buttonType"
        id="saveDraft"
        value="Save Draft"
        class="mr1"
      />
      <input type="reset" name="buttonType" value="Reset" class="mx1" />
      <input
        type="submit"
        name="buttonType"
        id="submit"
        value="Continue"
        class="ml1"
      />
    </div>
    <div id="error" style="color: red"></div>

    <table>
      <thead>
        <tr>
          <th class="nowrap text-left" colspan="2">Section 1</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>A1.</td>
          <td class="nowrap form-radio">
            <label>
              <input name="CriteriaA1" type="radio" value="Yes" /><span
                >Yes</span
              ></label
            ><label class="ml3"
              ><input name="CriteriaA1" type="radio" value="No" /><span
                >No</span
              ></label
            >
          </td>
        </tr>
        <tr>
          <td>A2.</td>
          <td class="nowrap form-radio">
            <label>
              <input name="CriteriaA2" type="radio" value="Yes" /><span
                >Yes</span
              ></label
            ><label class="ml3"
              ><input name="CriteriaA2" type="radio" value="No" /><span
                >No</span
              ></label
            >
          </td>
        </tr>
        <tr>
          <td>A3.</td>
          <td class="nowrap form-radio">
            <label>
              <input name="CriteriaA3" type="radio" value="Yes" /><span
                >Yes</span
              ></label
            ><label class="ml3"
              ><input name="CriteriaA3" type="radio" value="No" /><span
                >No</span
              ></label
            >
          </td>
        </tr>
        <tr>
          <td>A4.</td>
          <td class="nowrap form-radio">
            <label>
              <input name="CriteriaA4" type="radio" value="Yes" /><span
                >Yes</span
              ></label
            ><label class="ml3"
              ><input name="CriteriaA4" type="radio" value="No" /><span
                >No</span
              ></label
            >
          </td>
        </tr>
        <tr>
          <td>A5.</td>
          <td class="nowrap form-radio">
            <label>
              <input name="CriteriaA5" type="radio" value="Yes" /><span
                >Yes</span
              ></label
            ><label class="ml3"
              ><input name="CriteriaA5" type="radio" value="No" /><span
                >No</span
              ></label
            >
          </td>
        </tr>
      </tbody>
    </table>

    <p class="m2"><strong>OR</strong></p>

    <table>
      <thead>
        <tr>
          <th class="nowrap text-left" colspan="2">Section 2</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>B1.</td>
          <td class="nowrap form-radio">
            <label>
              <input name="CriteriaB1" type="radio" value="Yes" /><span
                >Yes</span
              ></label
            ><label class="ml3"
              ><input name="CriteriaB1" type="radio" value="No" /><span
                >No</span
              ></label
            >
          </td>
        </tr>
        <tr>
          <td>B2.</td>
          <td class="nowrap form-radio">
            <label>
              <input name="CriteriaB2" type="radio" value="Yes" /><span
                >Yes</span
              ></label
            ><label class="ml3"
              ><input name="CriteriaB2" type="radio" value="No" /><span
                >No</span
              ></label
            >
          </td>
        </tr>
        <tr>
          <td>B3.</td>
          <td class="nowrap form-radio">
            <label>
              <input name="CriteriaB3" type="radio" value="Yes" /><span
                >Yes</span
              ></label
            ><label class="ml3"
              ><input name="CriteriaB3" type="radio" value="No" /><span
                >No</span
              ></label
            >
          </td>
        </tr>
      </tbody>
    </table>

    <p class="m2"><strong>OR</strong></p>

    <table>
      <thead>
        <tr>
          <th class="nowrap text-left" colspan="2">Section 3</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>C1.</td>
          <td class="nowrap form-radio">
            <label>
              <input name="CriteriaC1" type="radio" value="Yes" /><span
                >Yes</span
              ></label
            ><label class="ml3"
              ><input name="CriteriaC1" type="radio" value="No" /><span
                >No</span
              ></label
            >
          </td>
        </tr>
        <tr>
          <td>C2.</td>
          <td class="nowrap form-radio">
            <label>
              <input name="CriteriaC2" type="radio" value="Yes" /><span
                >Yes</span
              ></label
            ><label class="ml3"
              ><input name="CriteriaC2" type="radio" value="No" /><span
                >No</span
              ></label
            >
          </td>
        </tr>
        <tr>
          <td>C3.</td>
          <td class="nowrap form-radio">
            <label>
              <input name="CriteriaC3" type="radio" value="Yes" /><span
                >Yes</span
              ></label
            ><label class="ml3"
              ><input name="CriteriaC3" type="radio" value="No" /><span
                >No</span
              ></label
            >
          </td>
        </tr>
        <tr>
          <td>C4.</td>
          <td class="nowrap form-radio">
            <label>
              <input name="CriteriaC4" type="radio" value="Yes" /><span
                >Yes</span
              ></label
            ><label class="ml3"
              ><input name="CriteriaC4" type="radio" value="No" /><span
                >No</span
              ></label
            >
          </td>
        </tr>
        <tr>
          <td>C5.</td>
          <td class="nowrap form-radio">
            <label>
              <input name="CriteriaC5" type="radio" value="Yes" /><span
                >Yes</span
              ></label
            ><label class="ml3"
              ><input name="CriteriaC5" type="radio" value="No" /><span
                >No</span
              ></label
            >
          </td>
        </tr>
        <tr>
          <td>C6.</td>
          <td class="nowrap form-radio">
            <label>
              <input name="CriteriaC6" type="radio" value="Yes" /><span
                >Yes</span
              ></label
            ><label class="ml3"
              ><input name="CriteriaC6" type="radio" value="No" /><span
                >No</span
              ></label
            >
          </td>
        </tr>
      </tbody>
    </table>

    <div class="rate-buttons left py2">
      <input type="reset" name="buttonType" value="Reset" class="mx1" />
      <input
        type="submit"
        name="buttonType"
        id="submit"
        value="Continue"
        class="ml1"
      />
    </div>
    <div id="error" style="color: red"></div>

    <!-- to here -->
  </form>




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签