2011-08-26 11 views
0
$('#err input').bind('blur keyup',function() { 
     $(this).data('errors', "ABC " + $(this).data('err') + " " + $(this).val()); 
    }); 

$('#go').click(function(){ 
    var code = ""; 
    $("#err input").each(function(){ 
    code += $(this).data('errors') + "\n" || ''; 
    }); 
    $('#x').html(code); 
}); 

HTML:入力が空になる場合は、検証を行うための方法jQueryの検証入力

<div id="err"> 
    <input data-err="A" id="e-1" type="text"/> 
    <input data-err="B" id="e-2" type="text"/> 
    <input data-err="C" id="e-3" type="text"/> 
</div> 

、この入力が処理されますか?

答えて

1

この

$('#go').click(function(){ 
    var code = ""; 
    $("#err input").each(function(){ 
    //This condition will check for empty values 
    if($(this).val()){ 
     code += $(this).data('errors') + "\n" || ''; 
    } 

    }); 
    $('#x').html(code); 
}); 
+0

作品、感謝してみてください:) –

0

送信ハンドラからfalseを返すと、デフォルトのフォーム送信機能がキャンセルされます。だから、使用:

$('#go').submit(function(){ 
    //do validation here 
    if(validationFails)return false; 
});