【问题标题】:Dropdown box / radio button validation?下拉框/单选按钮验证?
【发布时间】:2011-06-07 20:00:48
【问题描述】:

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

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

【问题讨论】:

  • 在 [documentation][(flowplayer.org/tools/demos/validator/events.html) 中有一个明确的示例验证复选框,您到目前为止做了什么向我们展示您的结果(代码)以更好地帮助您
  • 抱歉,错字,不想要复选框...需要它来处理下拉菜单和单选按钮!

标签: jquery validation drop-down-menu radio-button jquery-tools


【解决方案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>

【讨论】:

    【解决方案2】:

    在文档中它说您也可以对 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;
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-29
      • 1970-01-01
      • 2011-11-27
      • 2018-02-19
      • 1970-01-01
      • 2015-09-01
      • 2012-12-01
      相关资源
      最近更新 更多