0

我的表单中有一个部分,仅在选中复选框时才会显示。因此,我对 tho 字段的验证仅在选中复选框时才适用。我的代码不起作用。

这是我到目前为止得到的,并在以下位置找到了这些信息:

http://docs.jquery.com/Plugins/Validation

// Validation

function validateForm() {
    $('#frmMain').validate({
        rules: {

            SuretyFullName: { required: '#chkSurety:checked' } //if required: true it works all the time.
        },
        messages: {
            SuretyFullName: { required: 'Required' }
        },

        submitHandler: function (form) {
            $('#btnSubmit').click();

        }
    });
}

代码编辑:以下建议:表单变得无响应:

$(document).ready(function(){
  $("#btnSubmit").click(function(){
if($('#chkSurety').is(":checked"))
{  
function validatesurety(){
 $('#frmMain').validate({
 rules: {       
            SuretyFullName: { required: true } 
            },
        messages: {
            SuretyFullName: { required: 'Required' } 
        }
    });
}
4

3 回答 3

2

你可以检查它是否检查然后运行你的函数

  $(document).ready(function(){
 //the submit click 
      $("#submit").click(function(){
    if($('#checkboxid').is(":checked"))// check if the checkbox checked if yes 
    {
    //then call you function 
    function validateForm() ;
    }
      });
    });
于 2012-05-17T05:48:58.683 回答
2

把它放在你的validateForm函数中:

// set the default rules
var rules = {
    SuretyFullName: { required: false },
    SuretyIDNumber: { required: true}, 
    SuretyPhysicalAddress: { required: true }, 
    SuretyHomeTel: { required: true}, 
    SuretyPlace: { required: true}, 
    SuretyDay: { required: true }, 
    SuretyMonth: { required: true } 
 };

 // override the full name rule, based on the checkbox
 rules.SuretyFullName.required = $('#chkSurety').is(':checked');

 $('#fmrMain').validate({
      rules: rules,
      ...
 });
于 2012-05-18T10:22:55.953 回答
1
function validateForm() {
    $('#frmMain').validate({
        rules: {

           SuretyFullName: { required: function (element) { if ($('#chkSuretyShip').is(':checked')) { return true } else { return false} } },           
        },
        messages: {
            SuretyFullName: { required: 'Required' }
        },

        submitHandler: function (form) {
            $('#btnSubmit').click();

        }
    });
}    
于 2012-05-18T13:05:00.790 回答