2011-01-12 97 views
0

我使用jQuery工具(http://flowplayer.org/tools/demos/index.html)验证器为我的表单,并想知道如何验证下拉菜单和单选按钮?下拉框/单选按钮验证?

任何人都有实施它的经验?

+0

有一个明显的例子为验证[文档】在一个复选框[(http://flowplayer.org/tools/demos/validator/events .html)你到目前为止做了什么向我们展示你的结果(代码)以更好地帮助你 – ifaour 2011-01-12 13:36:19

+0

对不起,错字,不希望复选框...需要它在下拉和单选按钮上工作! – 2011-01-12 13:39:17

回答

0

在文档中说它也可以对SELECT元素使用required =“required”属性,但它对我也不适用。 我选择使用自定义验证器功能。希望这对你有用。这是非常基本的,但它缺少一些其他考虑因素使其更加灵活。

$.tools.validator.fn("select[required=required]", function(input, value) { 
    // If the first item in the list is selected return FALSE 
    return !input[0].options[0].selected; 
}); 
1

只需添加它像这样:

<script type="text/javascript"> 
$(document).ready(function(){ 
    // initialize validator and supply the onBeforeValidate event in configuration 

$("#GetAQuote").validator({ 
    position: 'top left', 
    offset: [-5, 25], 
    message: '<div><em/><img src="images/form-error.gif" style="float:right;"/></div>'  
    // em element is the arrow 
}); 

$.tools.validator.fn("select[required=required]", function(input, value) { 
    // If the first item in the list is selected return FALSE 
    return !input[0].options[0].selected; 
}); 


$("#GetAQuote").bind("onFail", function(e, errors) { 

// we are only doing stuff when the form is submitted 
if (e.originalEvent.type == 'submit') { 

    // loop through Error objects and add the border color 
    $.each(errors, function() { 
     var input = this.input; 
     input.css({borderColor: '#e6a200'}).focus(function() { 
      input.css({borderColor: '#75a9cc'}); 
     }); 
    }); 
    } 
    }); 
}); 
</script>