简体   繁体   English

jQuery验证来自grails中的非必需字段

[英]Jquery validate from with non require fields in grails from

I got a form, input field and select box. 我得到了一个表格,输入字段和选择框。 Now I'd like to check if people input to the field, they also need to check the value from select box before submitting the form. 现在,我想检查是否有人在字段中输入内容,他们还需要在提交表单之前检查选择框的值。 Otherwise just submit. 否则,只需提交即可。

Here my code: 这是我的代码:

<g:form action="saveInfo2" id="applicantForm">
    <input id="checkDL1" type="text" name="forJobsRequiringUseOfAMotorVehicleDlNo1"/>
    <select id="checkState1">
        <option value=''></option>
        <option value='1'></option>
        <option value='2'></option>
    </select>
</g>

Try this: 尝试这个:

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
    </script>
    <script>
$(function () {
$("#submit").click(function ()  //submit button click
    {
     var inputval=$('#checkDL1').val();
     var selval=$('#checkState1').val(); 
         if(inputval=="")
            {
                alert("pllease enter text field");
                return false;
         }   
         if(selval==0)
            { 
                alert("pllease select option");
                    return false;
            }
        else{ 
        return true;
        }

        });   
        });  
    </script> 

<g:form action="saveInfo2" id="applicantForm">
    <input id="checkDL1" type="text"  name="forJobsRequiringUseOfAMotorVehicleDlNo1"/>
    <select id="checkState1">
        <option value='0'>select</option>
        <option value='1'>hi</option> 
        <option value='2'>hello</option>

    </select>
        <input  type="submit" id="submit" name="submit"/>
</g> 

http://jsfiddle.net/k6jbu/ http://jsfiddle.net/k6jbu/

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

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