2010-12-21 108 views
0

为什么这对某些人而言并不适用于其他人?其他人只需点击按钮,它不起作用。它在我添加了nospaces验证方法后开始这样做。任何建议将非常感激。jQuery验证在某些浏览器上工作,但不在其他浏览器上

<form method="post" action="#" id="client_form" class="order-form"> 
<input type="text" id="user_name" name="user_name" class="req nospaces" /> 
</form> 
<button id="client_next_btn"><?php echo($content->getString('order')); ?></button> 
<script type="text/javascript"> 

$(function() 
{ 
jQuery.validator.addClassRules('req', { required: function (el) { return $(el).is(':visible') } }); 

var nums = ['0','1','2','3','4','5','6','7','8','9']; 
var letters = ['q','w','e','r','t','y','u','i','o','p','a','s','d','f','g','h','j','k','l','z','x','c','v','b','n','m']; 
var alphanumeric = nums.concat(letters); 

jQuery.validator.addMethod("nospaces", function(value, element) { 
    var val = value.split(""); 
    var bool = true; 
    for (var i in val) 
    { 
     if (jQuery.inArray(val[i], alphanumeric) == -1) 
     { 
      bool = false; 
      break; 
     } 
    } 
    return bool; 
}, "* <?php echo ($content->getString('no_spaces')); ?>"); 

$('#client_form').validate(); 
$('#client_next_btn').click(function() 
{ 
    $('#client_message').empty(); 

    if ($('#client_form').validate().form()) 
    { 
       alert('works'); 
      } 
    } 

}

+0

js无法修改并调整它直到没有错误,除非您复制它不好 – qwertymk 2010-12-21 06:44:19

回答

0

刚过我张贴的问题,我想出了答案 - addMethod必须与addClassRules一起去,而不是通过自身

1

就像一个提示,你可以摆脱这样的:

jQuery.validator.addClassRules('req', { 
    required: function (el) { return $(el).is(':visible') } 
}); 

,并使用内置required类,只需添加ignore option到您的通话.validate()排除:hidden元素,如下所示:

$('#client_form').validate({ ignore: ':hidden' }); 
相关问题