防止对隐藏字段进行spry验证

时间:2013-01-26 13:09:35

标签: javascript jquery validation hidden-field spry

我有一个表格,其中spry验证应用于某些元素。

在此表单上也是一个下拉列表,根据所选内容隐藏某些元素。

最初,spry验证代码位于页面底部,但由于验证的表单字段不再存在,因此无法提交表单。 我从以下开始:

function checkPageType(myType){
    switch (myType) {
            case 'Standard Page':
            $('.slugDiv').show();
            break;
            case 'Borough Menu':
            $('.slugDiv').hide();
            break;
        case 'Area Menu':
            $('.slugDiv').hide();
        case 'Forms Menu':
            $('.slugDiv').hide();
        break;  
        }
    }   
    $('#idptp_sec').change(function() {
        var myVal = $(this).val();
        checkPageType(myVal);
    });

在我的页面底部

<script type = "text/Javascript">
var slug_sec_Spry = new Spry.Widget.ValidationTextField("slug_sec_Spry", "none",{validateOn:["blur"], characterMasking: /^[a-z0-9-]+$/, useCharacterMasking:true});
</script>

我已经尝试将其移动到我的功能中,但它仍然不起作用,我猜测因为它只有功能范围可能吗?

function checkPageType(myType){
    switch (myType) {
        case 'Standard Page':
            $('.slugDiv').show();
            var slug_sec_Spry = new Spry.Widget.ValidationTextField("slug_sec_Spry", "none",{validateOn:["blur"], characterMasking: /^[a-z0-9-]+$/, useCharacterMasking:true});
        break;
        case 'Borough Menu':
            $('.slugDiv').hide();
            slug_sec_Spry.destroy();
            slug_sec_Spry= null;
        break;
        case 'Area Menu':
            $('.slugDiv').hide();
            slug_sec_Spry.destroy();
            slug_sec_Spry= null;
        case 'Forms Menu':
            $('.slugDiv').hide();
            slug_sec_Spry.destroy();
            slug_sec_Spry= null;
        break;  
    }
}   
$('#idptp_sec').change(function() {
    var myVal = $(this).val();
    checkPageType(myVal);
});

那么,如果在页面加载后隐藏了字段,我怎么能阻止该字段被验证?

更新:

我一直在尝试在这里找到的其他解决方案: Spry validation prevents the form submission on unavailable fields

我已将代码更改为:

var slug_sec_Spry =  Spry.Widget.ValidationTextField("slug_sec_Spry", "none",{validateOn:["blur"],characterMasking: /^[a-z0-9-]+$/, useCharacterMasking:true});

    function clearValidation(){
    if(slug_sec_Spry){
        slug_sec_Spry.reset();
        slug_sec_Spry.destroy()
    }
}

function reapplyValidation(){
    slug_sec_Spry = new Spry.Widget.ValidationTextField("slug_sec_Spry", "none",{validateOn:["blur"],characterMasking: /^[a-z0-9-]+$/, useCharacterMasking:true});
}
    function checkPageType(myType){
        switch (myType) {
            case 'Standard Page':
                $('.slugDiv').show();
                reapplyValidation()
            break;
            case 'Borough Menu':
                $('.slugDiv').hide();
                clearValidation()
            break;
            case 'Area Menu':
                $('.slugDiv').hide();
                clearValidation()
            case 'Forms Menu':
                $('.slugDiv').hide();
                clearValidation()
            break;  
        }
    }   
    $('#idptp_sec').change(function() {
        var myVal = $(this).val();
        checkPageType(myVal);
    });

但它们会生成TypeError:this.isBrowserSupported is not a function

1 个答案:

答案 0 :(得分:0)

现在正在工作,只需要一些错误检查代码......

<script type="text/javascript">
$(document).ready(function() {
    var  isHidden = false;

    function clearValidation(){
        slug_sec_Spry.reset();
        slug_sec_Spry.destroy();
        $('.slugDiv').hide();
        isHidden = true;

    }
    function reapplyValidation(){
        $('.slugDiv').show();
        slug_sec_Spry = new Spry.Widget.ValidationTextField("slug_sec_Spry", "none",{validateOn:["blur"], characterMasking: /^[a-z0-9-]+$/, useCharacterMasking:true});
        isHidden = false;
    }
    function checkPageType(myType){
        switch (myType) {
            case 'Standard Page':
                if(isHidden == true){
                    reapplyValidation();
                }

            break;
            case 'Borough Menu':
                if(isHidden == false){
                    clearValidation()
                }
            break;
            case 'Area Menu':     
                if(isHidden == false){
                    clearValidation()
                }
            case 'Forms Menu':
                if(isHidden == false){
                    clearValidation()
                }
            break;  
        }
    }   
    $('#idptp_sec').change(function() {
        var myVal = $(this).val();
        checkPageType(myVal);
    });
    //perform initial check
    var initVal = $('#idptp_sec').val();
    checkPageType(initVal);
});
</script>