简体   繁体   English

如何通过表单验证启用取消按钮?

[英]How do I enable cancel button with form validation?

I'm using the frmvalidator javascript found here for my code. 我正在使用在这里找到的frmvalidator javascript代码。

I had to create a custom validation because I'm validating two input fields with the same id (I'm using an array). 我必须创建一个自定义验证,因为我要验证两个具有相同ID的输入字段(我正在使用数组)。 The validation is working, the problem now though is when I click the cancel button, the validation still takes effect meaning I can't close the dialog box unless I fill in all fields. 验证有效,但是现在的问题是,当我单击“ cancel按钮时,验证仍然生效,这意味着除非填写所有字段,否则无法关闭dialog box

当我单击取消时,此错误消息仍然出现。

Here's my code: 这是我的代码:

<form name="UpdateIndustry" action="addIndustry.jsp" method="get">
  <table align="center">                
    <c:forEach var="row" items="${paramValues.industries}">
      <jsp:setProperty property="tableId" name="bean" value="${row}"/>
      <c:set var="ctr" value="${ctr + 1}"/>
      <input type="hidden" name="hello" value="${row}"/>
      <tr><th>INDUSTRY NAME</th>
          <td><div id='UpdateIndustry_industryName_errorloc' style="color:red;"></div>
              <input type="text" name="industryName" size=40 value="${bean.thisIndustry.INDUSTRYNAME}"/></td></tr>
    </c:forEach>
    <input type="hidden" name="finalCount" value="${ctr}"/>
    <tr><td style="text-align:center" colspan="3">
        <input type=submit name="submit" value="Update Industry" onclick="btnUpdate_click()"/>
        <input type=submit name="submit" value="Cancel" onclick="btnCancel_click()"/></td></tr>
  </table>  
</form>

and the script : script

<script language="JavaScript">
    var frmvalidator  = new Validator("UpdateIndustry");
    frmvalidator.EnableOnPageErrorDisplay();
    frmvalidator.EnableMsgsTogether();
    function btnUpdate_click(){
        frmvalidator.setAddnlValidationFunction(FinalValidator); }                  
    function btnCancel_click(){
        frmvalidator.clearAllValidations();
        window.close(); }   
</script>

and here's the FinalValidator function, where the validation happens. 这是进行验证的FinalValidator函数。

function FinalValidator()
{
   var myForm = document.forms["UpdateIndustry"];
   if (myForm.industryName.length > 0) {
      for(var i=0; i <= myForm.industryName.length; i++)
        {
          if(myForm.industryName[i].value.length == 0)
            {
              sfm_show_error_msg('Please enter industry name.', myForm.industryName[i]);
              return false;
            }
        }
   }
   return true;
}

How can I get the cancel button to work? 如何使cancel按钮起作用? Thanks in advance for the help! 先谢谢您的帮助! :) :)

$(document).ready(function () {
    $("#YourButtonID").click(function () {
        error = false;
        $(".span1").remove();
        if ($("#YourTextBoxID").val() == "") {
            $("#YourTextBoxID").after("<span class='span1'>Write Your Error</span>");
            error = true;
        }
    });
});

Try this. 尝试这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM